0

我编写了一个 ios 应用程序,以便将数据保存到核心数据,然后使用以下代码获取它:

   NSManagedObjectContext *context = [self managedObjectContext];
NSError *error;

NSManagedObject *failedBankInfo = [NSEntityDescription
                                   insertNewObjectForEntityForName:@"UserData"
                                   inManagedObjectContext:context];
[failedBankInfo setValue:[NSNumber numberWithInt:1] forKey:@"userId"];

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
                               entityForName:@"UserData" inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
for (NSManagedObject *info in fetchedObjects) {
     NSLog(@"Name: %@", [info valueForKey:@"userId"]);

}

这里没有问题。但是在我关闭我的应用程序并将 setvalue 修改为 2 后,我只收到最后一个数据,即 2。之前的数据 (1) 被删除。

即使在我关闭我的应用程序后,我应该怎么做才能保留数据条目。

谢谢你!

4

2 回答 2

2

您需要创建一个UIManagedDocument来保存Core Data 信息。我通常创建UIManagedDocument作为类的属性。例如,我创建了一个UIManagedDocument名为theManagedDocument

NSURL *url = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
url = [url URLByAppendingPathComponent:@"DocumentName"];

self.theManagedDocument = [[UIManagedDocument alloc] initWithFileURL:url];

if([[NSFileManager defaultManager] fileExistsAtPath:[url path]])
{
    [theManagedDocument openWithCompletionHandler:^(BOOL success){
        if(success) [self documentIsReady];
        if(!success) NSLog(@"Couldn't Open Document");
    }];
}
else 
{
    [theManagedDocument saveToURL:url forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success){
        if(success) [self documentIsReady];
        if(!success) NSLog(@"Couldn't Create Document");
    }];
}

然后我创建了一个方法documentIsReady,当文档被成功创建或打开时调用(如果它已经存在则打开,否则创建)。我还将上下文作为属性。这里称为context。我还在您的代码中添加了:

- (void) documentIsReady
{
    if(self.theManagedDocument.documentState == UIDocumentStateNormal) 
    {
        self.context = self.theManagedDocument.managedObjectContext;
        NSError *error;

        NSManagedObject *failedBankInfo = [NSEntityDescription
                               insertNewObjectForEntityForName:@"UserData"
                               inManagedObjectContext:self.context];
        [failedBankInfo setValue:[NSNumber numberWithInt:1] forKey:@"userId"];

        NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
        NSEntityDescription *entity = [NSEntityDescription
                           entityForName:@"UserData" inManagedObjectContext:self.context];
        [fetchRequest setEntity:entity];
        NSArray *fetchedObjects = [self.context executeFetchRequest:fetchRequest error:&error];
        for (NSManagedObject *info in fetchedObjects) {
            NSLog(@"Name: %@", [info valueForKey:@"userId"]);
        }
    }
}

然后,您将需要在完成使用后关闭文档:

[self.theManagedDocument closeWithCompletionHandler:^(BOOL success){
    if(success) NSLog(@"Closed Successfully");
    if(!success) NSLog(@"Error Closing Document");
}];
于 2012-07-11T21:53:36.750 回答
1

您没有保存新记录。

[self.managedObjectContext save:nil]; 
于 2012-07-12T07:37:53.777 回答