私には2つの方法がありますが、
最初の方法では値を 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;
}
私が間違っているところを案内してください。