ios - 崩溃 [上下文 executeFetchRequest :request error:&error] always in Core Data?

标签 ios objective-c core-data nsmanagedobjectcontext executefetchrequest

我运行我的应用程序,所以总是在 [context executeFetchRequest:request error:&error] 上崩溃,因为多个异步数据保存在核心数据中。那么如何处理这个问题呢?我正在尝试这段代码。

        NSManagedObjectContext *context =[appDelegate managedObjectContext];
        NSFetchRequest *request = [[NSFetchRequest alloc] init];
        NSEntityDescription *entity = [NSEntityDescription entityForName:@"Client" inManagedObjectContext:context];
        NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"clientId = %@",clientId]];
        [request setPredicate:predicate];
        [request setEntity:entity];

        NSError *error = nil;
        NSMutableArray *mutableFetchResults =(NSMutableArray *)[context executeFetchRequest:request error:&error];
        if (mutableFetchResults == nil)
        {
            NSLog(@"ERROR - %@", error);
        }
        if (mutableFetchResults!=nil && [mutableFetchResults count]>0)
        {
            return [mutableFetchResults objectAtIndex:0];
        }
        else
        {
            return nil;
        }

这是我的崩溃日志:

Terminating app due to uncaught exception 'NSGenericException', reason: '*** Collection <__NSCFSet: 0x7fac74d0a290> was mutated while being enumerated.'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010baa3e65 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x000000010b0c8deb objc_exception_throw + 48
    2   CoreFoundation                      0x000000010baa37c4 __NSFastEnumerationMutationHandler + 132
    3   CoreData                            0x0000000109f486ef -[NSManagedObjectContext executeFetchRequest:error:] + 2111
    4   CuztomisePharma                     0x0000000109acbec9 -[SyncManager getClientByClientId:] + 489
    5   CuztomisePharma                     0x0000000109ac9cf6 -[SyncManager saveClient:] + 214
    6   CuztomisePharma                     0x0000000109ac98a6 -[SyncManager firstTimeSync] + 2406
    7   CuztomisePharma                     0x0000000109a31029 -[LoginViewController loginServerCall] + 1273
    8   Foundation                          0x000000010ad52dfb __NSThread__start__ + 1198
    9   libsystem_pthread.dylib             0x000000010e37299d _pthread_body + 131
    10  libsystem_pthread.dylib             0x000000010e37291a _pthread_body + 0
    11  libsystem_pthread.dylib             0x000000010e370351 thread_start + 13
)
2016-05-25 14:23:08.682 CuztomisePharma[6615:146656] The selected car is: (
    "<Drug: 0x7fac75d18150> (entity: Drug; id: 0xd0000000002c001e <x-coredata://AE430EFF-FF79-48C9-BDB3-08707B8B172F/Drug/p11> ; data: <fault>)"
)
libc++abi.dylib: terminating with uncaught exception of type NSException

最佳答案

NSManagedObjectContext 不是线程安全的。如果在 main thread 上创建上下文,则只能在 main thread 上访问上下文。

因此您必须在主线程中运行 executeFetchRequest,您可以使用 performBlock 来安排它在自己的线程上运行。

[context performBlock:^{
    NSMutableArray *mutableFetchResults =(NSMutableArray *)[context executeFetchRequest:request error:&error];

}];

注意:如果您在旧的 NSConfinementConcurrencyType 限制模型中初始化上下文,则 performBlock 将不起作用。

希望这对您有所帮助。

关于ios - 崩溃 [上下文 executeFetchRequest :request error:&error] always in Core Data?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37431938/

相关文章:

ios - Firebase 值未更新?

ios - xcode 5.1 不断修改我的 Storyboard

ios - 将现有的核心数据迁移到 iCloud

objective-c - iOS:在核心数据中存储缩略图

ios - 二元运算符 + 不能应用于 CGfloat int 类型的操作数

ios - 将嵌套的 JSON 解析为 NSDictionary

ios - Affdex iOS SDK "set the licensePath property with a valid file path"错误

ios - 带有 iCloud 的 CoreData : Receiving empty data on NSPersistentStoreDidImportUbiquitousContentChangesNotification

objective-c - 引用自身和实例变量的 block

swift - ObjectMapper 如何在 Swift 中映射时更改数据类型