gpt4 book ai didi

iphone - 关闭应用程序后数据被删除 [IOS]

转载 作者:行者123 更新时间:2023-11-28 20:33:27 27 4
gpt4 key购买 nike

我编写了一个 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"]);

}

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

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

谢谢!

最佳答案

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

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");
}];

关于iphone - 关闭应用程序后数据被删除 [IOS],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11441817/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com