对于在核心数据usihg中执行批量插入/更新/删除的iOS应用程序,usihg MagicalRecord:使用saveWithBlockAndWait将执行任何批保存以持久存储吗?根据苹果的建议,保存操作必须分批完成(比如将50或100条记录保存到磁盘),以避免内存增加。一定要打电话
[lcoalContext save:nil];在saveWithBlockAndWait内部有一定的间隔吗?这里,lcoalContext是从saveWithBlockAndWait方法中获得的NSManagedObjectContext的一个实例。
发布于 2014-11-05 15:24:24
这是我对你的需求的建议:
我直接为你写了一个简短的例子:
- (void) batchProcessToCreate1000Objects{
//This will create a separated context which is the child of the main context. You can use MR_newPrivateQueueContext instead of this if you would like to use a fully separated context (it won't merge the changes into the main context).
NSManagedObjectContext* localContext = [NSManagedObjectContext MR_context];
for (int counterToObservWhenToSave = 0; counterToObservWhenToSave < 1000; counterToObservWhenToSave++) {
YourManagedObject* objectToInsert = [NSManagedObject MR_createEntityInContext:localContext];
objectToInsert.name = @"Chris P. Backon";
objectToInsert.isStackOverFlowExample = @YES;
if (counterToObservWhenToSave > 0 && (counterToObservWhenToSave % 50 == 0)) {
[localContext MR_saveToPersistentStoreAndWait];
}
}
}还有,请注意:永远不要调用saveWithBlockAndWait函数中的save:。此函数将自动保存(如果您在块中做了任何CD更改)。
祝你编码好运!
https://stackoverflow.com/questions/20910902
复制相似问题