multithreading - iOS 8.3上的CoreData多线程崩溃

标签 multithreading core-data

在将设备/xCode更新到iOS 8.3之后,当我执行后台更新时,我的应用程序开始崩溃。
这是我的managedObjectContext/persistentStoreCoordinator方法:

- (NSManagedObjectContext *)managedObjectContext {
    NSThread *thisThread = [NSThread currentThread];
    if (thisThread == [NSThread mainThread]) {
        return self.mainContext;
    }

    NSString * threadKey = [NSString stringWithFormat:@"%p", thisThread];
    NSManagedObjectContext * threadContext = nil;
    @synchronized(self.managedObjectContexts) {
        threadContext = self.managedObjectContexts[threadKey];
    }

    if (!threadContext) {
        threadContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
        threadContext.parentContext = self.mainContext;
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(contextDidSave:) name:NSManagedObjectContextDidSaveNotification object:threadContext];

        @synchronized(self.managedObjectContexts) {
            [self.managedObjectContexts setObject:threadContext forKey:threadKey];
        }
    }

    return threadContext;
}

+ (NSString*)managedObjectContextKeyForThread:(NSThread*)thread {
return [NSString stringWithFormat:@"%i", (int)thread];
}

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
if (!persistentStoreCoordinator) {
    NSManagedObjectModel *objectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:self.modelURL];

    if(objectModel) {
        NSPersistentStoreCoordinator *storeCoordinator = [[NSPersistentStoreCoordinator alloc]
                                                          initWithManagedObjectModel:objectModel];
        NSDictionary * options = @{NSMigratePersistentStoresAutomaticallyOption : @(YES),
                                   NSInferMappingModelAutomaticallyOption : @(YES)
                                   };
        NSError* error = nil;
        if ([storeCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:self.storeURL
                                                 options:options error:&error])
        {
            persistentStoreCoordinator = storeCoordinator;
        } else {
            NSError *err = nil;
            [[NSFileManager defaultManager] removeItemAtURL:self.storeURL error:&err];
            if ([storeCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:self.storeURL
                                                     options:options error:&error])
            {
                [[NSNotificationCenter defaultCenter] postNotificationName:kDatabaseRecreatedNotificationKey object:nil];
                persistentStoreCoordinator = storeCoordinator;
            }
        }
    }
}

return persistentStoreCoordinator;
}

它在contextDidSave方法中崩溃,并显示错误消息:

在Debug Navigator中的第一个异常断点处,它显示了另外一行-“developerSubmittedBlockToNSManagedObjectContextPerform”

我的代码有什么问题?在8.3之前,它运行良好。也许,苹果添加了新规则?
感谢您的帮助。

最佳答案

8.3的发布对我来说也是如此。如果您的情况像我的一样,那么您可以在viewDidLoad中设置self.mainContext(您没有显示该ivar的设置位置)。但是我可以在调试器中看到,现在在viewDidLoad之前调用了fetchedResultsController。我的解决方法是将等效于self.mainContext的设置移动到init方法中,在本例中为initWithCoder。

关于multithreading - iOS 8.3上的CoreData多线程崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29563510/

相关文章:

multithreading - 即时通讯服务器设计

iphone - 核心数据分段 TableView 排序错误

ios - 使用 UITableView Cell 中的 segue 更新数据

ios - 在后台线程上保存核心数据会导致获取死锁和崩溃

java - 如何Junit测试一个同步对象不能同时被两个线程访问?

iphone - Core Data iPhone 我应该多久调用一次[managementObjectContext save :&error] when doing 50k record insert?

iphone - 撤消在 subview Controller 中所做的所有更改

swift - 核心数据+本地化

multithreading - 在 Redis 中更新和检索 key

具有多个对象/锁的 Java 同步