简体   繁体   English

核心数据。 不通过应用程序终止持续存在

[英]Core data. Not persisting through app termination

So, i finally got Core Data to work as intended.所以,我终于让 Core Data 按预期工作了。 Saving stuff, and loading it within a UITableView subclass.保存内容,并将其加载到 UITableView 子类中。 Works perfectly as long as the application is running or in the background.只要应用程序正在运行或在后台运行,就可以完美运行。

But as soon as the app is terminated (not uninstalled), and reopened, the data seems lost, or rather reset to nothing(when i load the NSManagedObjects to my array, i still get the correct amount - the properties are just nil)...但是,一旦应用程序终止(未卸载)并重新打开,数据似乎会丢失,或者重置为空(当我将 NSManagedObjects 加载到我的数组时,我仍然得到正确的数量 - 属性只是零)。 ..

I save the context in:我将上下文保存在:

- (void)applicationDidEnterBackground:(UIApplication *)application

Besides that, the App-Delegate is 100% Core-Data boilerplate code, except for the method:除此之外,App-Delegate 是 100% Core-Data 样板代码,除了方法:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

I could paste some code, but i wouldn't know what might be interesting...我可以粘贴一些代码,但我不知道什么可能有趣......

Any suggestions?有什么建议么?

----------EDIT---------- - - - - - 编辑 - - - - -

Code for saving new instances:保存新实例的代码:

// create new screenshot and save
ScreenshotInfo *newScreenshot = [NSEntityDescription insertNewObjectForEntityForName:@"ScreenshotInfo" inManagedObjectContext:managedObjectContext];
newScreenshot.date = [[NSDate date] timeIntervalSince1970];
newScreenshot.note = @"click to add note...";
newScreenshot.thumbnailData = [self createThumbnailDataFromImage:image];
PicForScreenshot *newPic = [NSEntityDescription insertNewObjectForEntityForName:@"PicForScreenshot" inManagedObjectContext:managedObjectContext];
newScreenshot.pic = newPic;
newPic.image = screenshotData;
newPic.info = newScreenshot;
NSError *error;
if (![managedObjectContext save:&error]) {
    NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
}

Code for loading:加载代码:

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"ScreenshotInfo" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
NSError *error;
pics = [managedObjectContext executeFetchRequest:fetchRequest error:&error];
if (!pics) {
    [NSException raise:@"Fetch failed" format:@"reAson: %@",[error localizedDescription]];
}

Code for populating tableViewCells:填充 tableViewCells 的代码:

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
CellView *cell = [tableView dequeueReusableCellWithIdentifier:@"CellView"];
ScreenshotInfo *currentPic = [pics objectAtIndex:indexPath.row];

[[cell image]setImage:[UIImage imageWithData:currentPic.thumbnailData]];
[[cell noteLabel]setText:currentPic.note];
NSDate *date = [NSDate dateWithTimeIntervalSince1970:currentPic.date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MM/dd/yyyy hh:mma"];
[[cell dateLabel]setText:[dateFormatter stringFromDate:date]];
return cell;

} }

This should be the interesting code..这应该是有趣的代码..

The instances are created and saved in one ViewController, and loaded into another one (the UITableViewController).实例被创建并保存在一个 ViewController 中,然后加载到另一个 ViewController(UITableViewController)中。

@Tommy, you say you're sure that the saving is somehow wrong, but how can the TableView fetch it correctly immediately after creation, if the saving is not right. @Tommy,您说您确定保存在某种程度上是错误的,但是如果保存不正确,TableView 如何在创建后立即正确获取它。

1 - Create the object graphic representation inside the project's .xcdatamodeld file. 1 - 在项目的 .xcdatamodeld 文件中创建对象图形表示。 Set the attributes and relationships.设置属性和关系。

在此处输入图像描述

2 - Select the entity and click in Editor menu and select Create NSManagedObject Subclass... 2 - 选择实体并单击编辑器菜单并选择Create NSManagedObject Subclass...

在此处输入图像描述

3 - Create new entity instance by setting its context. 3 - 通过设置其上下文创建新实体实例。

var entity: Entity = Entity(context: self.context)

4 - Access/recover an existing NSManaged object graphic entity by the context calling the Persistent Store Coordinator method for the managed object graphic ID using its URL ID. 4 - 通过上下文调用Persistent Store Coordinator方法,使用其 URL ID 为托管对象图形 ID 访问/恢复现有的 NSManaged 对象图形实体。

let objectGraphID = self.context.persistentStoreCoordinator?.managedObjectID(forURIRepresentation: URL(string: "entityObjectGraphicID"))

核心数据栈关系

let nodeEntity = self.context.persistentStoreCoordinator?.managedObjectID(forURIRepresentation: objIDURL!)

Find more about Core Data here: https://cocoacasts.com/exploring-the-core-data-stack在此处查找有关核心数据的更多信息: https ://cocoacasts.com/exploring-the-core-data-stack

imageFont: https://cocoacasts.com/exploring-the-core-data-stack imageFont: https ://cocoacasts.com/exploring-the-core-data-stack

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM