0

我运行我的应用程序所以总是崩溃,[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
4

3 回答 3

1

您是否同时进行两项操作?一个枚举它,另一个将保存。

于 2016-05-25T11:27:52.620 回答
1

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

所以你必须executeFetchRequest在主线程中运行,而不是你可以使用performBlock它将安排它在自己的线程上运行。

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

}];

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

希望这可以帮助。

于 2016-05-25T11:04:14.877 回答
0

-executeFetchRequest:返回可变数组。仅仅因为您将其转换为 aNSMutableArray并不能使其成为NSMutableArray.

首先,你不应该做一个可变数组,因为你没有改变它。

其次,你不应该铸造回报。如果你想要一个可变数组,你需要正确转换它:

NSMutableArray *mutableFetchResults = [[context executeFetchRequest:request error:&error] mutableCopy];

同样,永远没有理由在获取结果上使用可变数组。结果数组是不可变的。

您在问题中显示的错误与您显示的代码不匹配,因为该代码没有改变数组。

显示代码,-getClientByClientId:因为那是发生崩溃的地方。

于 2016-05-25T19:40:03.233 回答