我有两种方法,
在第一种方法中,我确实将值保存在 Core Data 中,而在其他方法中,我只是获取它们。
插入后,当我在同一方法中获取数据时,它会显示值,但是当我尝试在其他方法中获取时,如果返回 null。
我的保存方法是
-(void) saveloginData:(NSString *)facebookTok username:(NSString *)userName password:(NSString*)password flag:(NSString *)flag {
NSError *error;
NSManagedObjectContext *context = [self managedObjectContext];
NSFetchRequest * fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:[NSEntityDescription entityForName:@"SignIn" inManagedObjectContext:context]];
[fetchRequest setIncludesPropertyValues:NO]; //only fetch the managedObjectID
NSString *facebookTokenData = facebookTok;
NSString *usernameData = userName;
NSString *passwordData = password;
NSString *flagData = flag;
NSLog(@"Facebook Token%@\nUsername%@\npassword%@\nflat%@\n",facebookTokenData,usernameData,passwordData,flagData);
SignIn *signIn = [NSEntityDescription
insertNewObjectForEntityForName:@"SignIn"
inManagedObjectContext:context];
signIn.facebookToken = facebookTokenData;
signIn.username = usernameData;
signIn.password = passwordData;
signIn.flag = flagData;
NSEntityDescription *entity = [NSEntityDescription entityForName:@"SignIn"
inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSArray *fetchedArray = [context executeFetchRequest:fetchRequest error:&error];
for (SignIn *info in fetchedArray) {
\\ THis executes and shows values, proves that value are inserted.
NSLog(@"Name ~~~~ : %@", info.username);
NSLog(@"Password ~~~~~~~~ :%@", info.password);
NSLog(@"FLAG ~~~~~~~~~~~ %@",info.flag);
NSLog(@"Facebook Token %@", info.facebookToken);
}
}
我的检索方法是
-(NSArray*) getLoginData {
NSError *error;
NSManagedObjectContext *context = [self managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"SignIn"
inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSArray *fetchedData = [[NSArray alloc] init];
fetchedData = [context executeFetchRequest:fetchRequest error:&error];
NSLog(@"The count of Array %d", [fetchedData count]); \\ HERE COUNT IS ZERO, WHY?
for (SignIn *info in fetchedData) {
NSLog(@" FF Name ~~~~ : %@", info.username);
NSLog(@"Password ~~~~~~~~ :%@", info.password);
NSLog(@"FLAG ~~~~~~~~~~~ %@",info.flag);
NSLog(@"Facebook Token %@", info.facebookToken);
}
return fetchedData;
}
请指导我做错的地方。