【发布时间】:2014-05-28 15:06:31
【问题描述】:
使用 MagicalRecord 在后台导入数据并在完成时访问刚刚导入的数据的适当方式是什么?我是否必须手动保存所有导入数据的主键并将其传递给完成块才能再次从 CoreData 中找到它们?
[MagicalRecord saveWithBlock:^(NSManagedObjectContext *localContext)
{
NSArray *importedEntitiesArray = [MyEntity MR_importFromArray:bigResultsArray
inContext:localContext];
} completion:^(BOOL success, NSError *error)
{
// How to access the imported entities from here?
// Note we no longer have access to the saving localContext
// so the entities in importedEntitiesArray would be invalid
}]
我意识到我可以做到以下几点:
NSArray *importedEntitiesArray = [MyEntity MR_importFromArray:bigResultsArray];
[[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreAndWait];
但这不会在后台导入数据。
【问题讨论】:
-
您想立即访问多少数据?您不想使用某些谓词进行批量提取吗?
-
理想情况下是所有刚获取的数据。这样做的原因是 API 知道根据服务器参数返回哪些实体,但是 CoreData 实体没有让我通过 fetch 谓词来计算它的属性(嗯,他们有,但它需要一些数据构建,我想知道是否可以避免构建这些数据)。
标签: objective-c core-data magicalrecord