有persistentStoreCoordinator的随机EX

有persistentStoreCoordinator的随机EX

我的应用有时会在以下行完全加载之前崩溃:

if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error])

该if状态所在的完整方法如下所示(我认为这很标准):
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
    if (__persistentStoreCoordinator != nil) {
        return __persistentStoreCoordinator;
    }

    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"CoreData.sqlite"];

    NSError *error = nil;
    __persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
    if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }

    return __persistentStoreCoordinator;
}

更新

崩溃发生在第二次调用-(NSPersistentStoreCoordinator *)persistentStoreCoordinator方法时。 编辑
第一个时间是从第一个可见的viewController中调用的:
- (void)updateStats {
    NSLog(@"Updating stats");
    dispatch_queue_t request_queue = dispatch_queue_create("updateNumberOfSchedules", NULL);
    dispatch_async(request_queue, ^{
         AppDelegate *theDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *context = [[NSManagedObjectContext alloc] init];
    [context setPersistentStoreCoordinator:[theDelegate persistentStoreCoordinator]];
    ...
    });
}

第二次(当我的DeviceLinker类要在我的checkInactiveLinks方法中检查数据库中的非事件链接时有时会发生崩溃的情况。该方法在applicationDidBecomeActive中启动时被调用:
-(void) checkInactiveLinks {
    AppDelegate *theDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *newMoc = [[NSManagedObjectContext alloc] init];
    [newMoc setPersistentStoreCoordinator:[theDelegate persistentStoreCoordinator]];
    ...
}

如果我错了,请纠正我,但阅读我的代码后,我会认为第二次调用persistentStoreCoordinator getter时,它应返回__persistentStoreCoordinator而不分配和初始化新的...

更新2
在if语句的同一行上,我有时也会得到这个:
-[__NSCFDictionary _hasPrecomputedKeyOrder]: unrecognized selector sent to instance 0x7dd3770

更新3

我编辑了构建方案,并在“诊断”选项卡下打开了僵尸和日志异常(exception)。现在我看到-[NSPersistentStoreCoordinator unlock]: message sent to deallocated instance 0x8916090。请注意,我的代码中没有任何显式锁。

最佳答案

看起来您正在同时从多个线程进入该代码段。进行惰性实例化时,必须确保仅通过一个执行线程的“第一次”将同时通过。您可以使用以下策略在主线程上同步对其的访问。

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
    if (__persistentStoreCoordinator != nil) {
        return __persistentStoreCoordinator;
    }

    // Add this block of code.  Basically, it forces all threads that reach this
    // code to be processed in an ordered manner on the main thread.  The first
    // one will initialize the data, and the rest will just return with that
    // data.  However, it ensures the creation is not attempted multiple times.
    if (![NSThread currentThread].isMainThread) {
        dispatch_sync(dispatch_get_main_queue(), ^{
            (void)[self persistentStoreCoordinator];
        });
        return __persistentStoreCoordinator;
    }

    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"CoreData.sqlite"];

    NSError *error = nil;
    __persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
    if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }

    return __persistentStoreCoordinator;
}

关于objective-c - 具有persistentStoreCoordinator的随机EXC_BAD_ACCESS,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10388724/

10-13 03:55