如何通过创建本地上下文将GCD与Core Data一起使用

时间:2015-09-03 05:59:15

标签: ios core-data

我正在尝试处理项目并将其与coredata和GCD一起存储。我正在将mainContext复制到localContext。我还没有进入合并部分,但它一直在崩溃我。这是我的代码:

dispatch_queue_t coreQueue = dispatch_queue_create("coreQueue", DISPATCH_QUEUE_CONCURRENT);

NSMutableSet __block *sumManagedObjects=[NSMutableSet set];

int processorCount = (int) [[NSProcessInfo processInfo] processorCount];

int __block limit = ceil((float)recordsToCreate.count/processorCount);
int loopCount = ceil((float)recordsToCreate.count/limit);
dispatch_apply(processorCount, coreQueue, ^(size_t i) {

    int startIndex = ((int)i)*limit;
    int maxIndex = (int)recordsToCreate.count;
    int endIndex = (startIndex + limit) < maxIndex ? (startIndex + limit) : maxIndex;
    int range = endIndex - startIndex;

    NSManagedObjectContext *localContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSConfinementConcurrencyType];
    [localContext setParentContext:self.mainContext];

NSSet *createdObjects=  [localContext insertWithEntityName:self.entityName withDataObjects:[NSSet setWithArray:[recordsToCreate subarrayWithRange:NSMakeRange(startIndex, range)]]];

 dispatch_async(dispatch_get_main_queue(), ^{

        [sumManagedObjects unionSet:createdObjects];
         NSLog(@"managed objects %tu start index %d end index %d size_t : %d, sum managed object : %tu thread %@", [createdObjects count], startIndex, endIndex, (int)i, [sumManagedObjects count], [NSThread currentThread]);
        if([sumManagedObjects count] == recordsToCreate.count) {
            [sumManagedObjects setValue:@YES forKey:ItemAttribute.someKey];[context save:nil];


                        }

    });

这是更新后的代码:

dispatch_queue_t coreQueue = dispatch_queue_create("coreQueue", DISPATCH_QUEUE_CONCURRENT);

NSMutableSet __block *sumManagedObjects=[NSMutableSet set];

int processorCount = 8;

int __block limit = ceil((float)recordsToCreate.count/processorCount);
int loopCount = ceil((float)recordsToCreate.count/limit);

NSLog(@"int count %d, record to create %tu", loopCount, recordsToCreate.count);

dispatch_apply(processorCount, coreQueue, ^(size_t i) {

    int startIndex = ((int)i)*limit;
    int maxIndex = (int)recordsToCreate.count;
    int endIndex = (startIndex + limit) < maxIndex ? (startIndex + limit) : maxIndex;
    int range = endIndex - startIndex;

     NSManagedObjectContext *privateManagedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
    [privateManagedObjectContext setPersistentStoreCoordinator:self.mainContext.persistentStoreCoordinator];
    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
    [nc addObserver:self selector:@selector(managedObjectContextDidSave:) name:NSManagedObjectContextDidSaveNotification object:privateManagedObjectContext];

    [privateManagedObjectContext insertWithEntityName:self.entityName withDataObjects:[NSSet setWithArray:[recordsToCreate subarrayWithRange:NSMakeRange(startIndex, range)]]];

    NSLog(@"current thread %@ start index %tu end index %tu size_t : %d", [NSThread currentThread], startIndex, endIndex, (int)i );


    dispatch_async(dispatch_get_main_queue(), ^{
        if(self.cancelled) return;

        [privateManagedObjectContext performBlockAndWait:^{
            NSError *error = nil;
            privateManagedObjectContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy;
            BOOL success = [privateManagedObjectContext save:&error];
            if (!success) {
                NSLog(@"%@", error);
            }
        }];



    });

});

- (void)managedObjectContextDidSave:(NSNotification *)notification {

[self.mainContext performBlockAndWait:^{
if(self.cancelled) return;
dispatch_async(dispatch_get_main_queue(), ^{
    if(self.cancelled) return;
    [self.mainContext mergeChangesFromContextDidSaveNotification:notification ];
});
}];}

现在我收到了这个错误: 由于未捕获的异常'NSGenericException'而终止应用程序,原因:'*** Collection&lt; __ NSCFSet:0x7fb5c30c3700&gt;在被列举时被突变。'

有什么想法吗?我是否正确阻止了资源?谢谢!

1 个答案:

答案 0 :(得分:1)

您无法在后台线程上更新限制上下文,然后将托管对象结果传递回主线程以执行另一次更新并保存上下文。所有更新和上下文保存都需要在后台线程上完成。这就是禁闭的意义,你需要遵守这种限制规则。