5

我有两种方法,

在第一种方法中,我确实将值保存在 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;
 }

请指导我做错的地方。

4

1 回答 1

4

您的问题是您需要保存 conext 以便以后获取实体。

NSManagedObjectContext save:尝试将对已注册对象的未保存更改提交到其持久存储。

- (BOOL)save:(NSError **)error

参数::error指向NSError对象的指针。您不需要创建NSError对象。如果您通过 ,则保存操作在第一次失败后中止NULL

保存成功则返回值YES,否则返回NO

因此,您需要在修改对象后保存上下文:

signIn.facebookToken = facebookTokenData;
signIn.username = usernameData;
signIn.password = passwordData;
signIn.flag = flagData;

[context save:NULL]; // NULL if you don't need to handle error
于 2013-07-12T10:39:19.340 回答