controllerDidChangeContent阻塞主线程

时间:2014-04-09 15:08:33

标签: ios objective-c multithreading core-data nsfetchedresultscontroller

当我尝试保存私有NSManagedObjectContext的数据时,即使在performBlock:块内调用此上下文的操作,我也会遇到死锁。这很奇怪,因为这个块应该在不中断主 UI / UX 线程的情况下异步执行。
以下是此方法的代码:

- (void)loadTimetableToCoreData:(id)timetable
{   
    // Initializing temporary context
    NSManagedObjectContext *tempContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
    tempContext.parentContext = self.moc;

    [tempContext performBlock:^{
      // Here is the parsing 

        NSLog(@"Finished loading to temp MOC");

        NSFetchRequest* r = [NSFetchRequest fetchRequestWithEntityName:@"Timetable"];
        [r setIncludesPendingChanges:NO];
        NSArray *existingTimetables = [tempContext executeFetchRequest:r error:nil];

        for (Timetable *table in existingTimetables) {
            [tempContext deleteObject:table];
        }

        // Saving procedure with multithreading
        NSError *error;
        if (![tempContext save:&error]) {
            NSLog(@"Couldn't save: %@", [error localizedDescription]);
        }

        NSLog(@"Finished saving to temp MOC");

        [self.moc performBlock:^{
            // Save groups to presistant store
            NSError *error;
            if (![self.moc save:&error]) {
                NSLog(@"Couldn't save: %@", [error localizedDescription]);
            }
            NSLog(@"Finished saving to main MOC");

            [self.writer performBlock:^{
                // Save groups to presistant store
                NSError *error;
                if (![self.writer save:&error]) {
                    NSLog(@"Couldn't save: %@", [error localizedDescription]);
                }
                NSLog(@"Finished saving to writer MOC");
            }];
        }];
    }];
}

正如你所看到的,这个方法中有一些NSLog并且可视化我的问题我遇到了已完成加载到临时MOC 已完成保存到临时MOC < / strong>即可。
我做错了什么以及如何解锁主线程?

更新

以下是乐器的屏幕以及我可以看到的内容...... controllerDidChangeCintent正在扼杀所有内容。 enter image description here 我知道这是由loadTimetableToCoreData:

中的那些行引起的
NSFetchRequest* r = [NSFetchRequest fetchRequestWithEntityName:@"Timetable"];
        [r setIncludesPendingChanges:NO];
        NSArray *existingTimetables = [tempContext executeFetchRequest:r error:nil];

        for (Timetable *table in existingTimetables) {
            [tempContext deleteObject:table];
        }

但我无法摆脱他们!或者我不知道如何......

0 个答案:

没有答案