我需要对核心数据执行大量的导入任务.
假设我的核心数据模型如下所示:
Car
----
identifier
type
Run Code Online (Sandbox Code Playgroud)
我从我的服务器获取汽车信息JSON列表,然后我想将它与我的核心数据Car对象同步,这意味着:
如果它是一辆新车 - > Car从新信息创建一个新的Core Data 对象.
如果汽车已经存在 - >更新Core Data Car对象.
所以我想在后台进行导入,而不会阻止UI,而使用滚动显示所有汽车的汽车表视图.
目前我正在做这样的事情:
// create background context
NSManagedObjectContext *bgContext = [[NSManagedObjectContext alloc]initWithConcurrencyType:NSPrivateQueueConcurrencyType];
[bgContext setParentContext:self.mainContext];
[bgContext performBlock:^{
NSArray *newCarsInfo = [self fetchNewCarInfoFromServer];
// import the new data to Core Data...
// I'm trying to do an efficient import here,
// with few fetches as I can, and in batches
for (... num of batches ...) {
// do batch import... …Run Code Online (Sandbox Code Playgroud)