我正在尝试NSManagedContext
通过嵌套上下文从一个移动到多个。
我正在使用这些文章来帮助我:
- http://www.cocoanetics.com/2012/07/multi-context-coredata/
- http://floriankugler.com/blog/2013/4/2/the-concurrent-core-data-stack
实际系统
[myHTTPClient getPath:path
parameters:@{access_token & stuff}
success:^(AFHTTPRequestOperation *operation, id responseObject) {
for (NSDictionary *dictionary in responseObject)
{
// filling the dic in a NSManagedObject
}
[myMainContext save:&error];
}, failure:failureBlock];
通缉系统
[myHTTPClient getPath:path
parameters:@{access_token & stuff}
success:^(AFHTTPRequestOperation *operation, id responseObject) {
__block NSManagedObjectContext *managedObjectContext = [(AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
__block NSManagedObjectContext *writerObjectContext = [(AppDelegate *)[[UIApplication sharedApplication] delegate] writerManagedObjectContext];
__block NSManagedObjectContext *temporaryContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
temporaryContext.parentContext = managedObjectContext;
NSEntityDescription *entity = [[self.fetchedResultsController fetchRequest] entity];
[temporaryContext performBlock:^{
for (NSDictionary *dictionary in responseObject)
{
// filling the dic in a NSManagedObject
}
[temporaryContext save:&error];
[managedObjectContext performBlock:^{
[managedObjectContext save:&error];
[writerObjectContext performBlock:^{
[writerObjectContext save:&error];
}];
}];
}];
}, failure:failureBlock];
图表
问题
使用此方法时我在保存数据时遇到问题,但我解决了(参见https://stackoverflow.com/questions/18151827/coredata-writermanagedobjectcontext-freeze-when-save)
但是,正如我在上一个问题中所说,它在模拟器上加速了 2 倍,但在设备上却慢了 5 倍。我使用 iPod Touch 4th 进行测试。
这怎么可能,我该如何改进?我不反对使用这个图表:
但是我想知道如何将它集成到我的代码中,因为我尝试过并且没有任何改变。
谢谢你。