gpt4 book ai didi

ios - 保存到 RKManagedObjectStore 的 mainQueueManagedObjectContext 的实体在下一次构建时消失

转载 作者:可可西里 更新时间:2023-11-01 06:23:30 25 4
gpt4 key购买 nike

使用 RestKit v0.20.0-rc1,我成功地创建了 CoreData 映射并从捆绑的 JSON 文件中导入对象,并让数据在多个构建中保持不变。但是,当我创建自己的实体并保存它时,如果我使用 [RKManagedObjectStore defaultStore].mainQueueManagedObjectContext,实体会在下一次构建时立即消失,但如果我使用 [RKManagedObjectStore defaultStore].persistentStoreManagedObjectContext,实体将正确保留。

    UserAccount *userAccount = [NSEntityDescription insertNewObjectForEntityForName:entityName inManagedObjectContext:managedObjectContext];
userAccount.userID = @(userID);
[userAccount addContactMethodsObject:phone];
NSError *error = nil;
if(![managedObjectContext save:&error])
NSLog(@"%@", error);

在上面的代码中使用任一 managedObjectContext 都不会出错,并且从同一上下文中提取的任何内容都会正确返回实体。但是在后续构建中,如果我使用 mainQueueManagedObjectContext,即使上面的代码在主线程上运行,fetches 也将始终返回 nil。

有什么我想念的吗?

最佳答案

当您在使用 mainQueueManagedObjectContext 创建的上下文中使用 save: 时,它不会将其更改保存到存储中。根据 RKManagedObjectStore 的文档(粗体):

The managed object context hierarchy is designed to isolate the main thread from disk I/O and avoid deadlocks. Because the primary context manages its own private queue, saving the main queue context will not result in the objects being saved to the persistent store. The primary context must be saved as well for objects to be persisted to disk.

如果您想保留您的更改并仍然使用 mainQueueManagedObject 上下文,请尝试使用

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

这会将更改提升到上下文层次结构中。

RKManagedObjectStore 的相关文档可以在 http://restkit.org/api/latest/Classes/RKManagedObjectStore.html#//api/name/persistentStoreManagedObjectContext 找到

restkit 的 NSManagedObject 类别的文档可以在以下位置找到 http://restkit.org/api/0.20.0-pre3/Categories/NSManagedObjectContext+RKAdditions.html

关于ios - 保存到 RKManagedObjectStore 的 mainQueueManagedObjectContext 的实体在下一次构建时消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15193025/

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