核心数据在枚举时发生变异

Gab*_*cal 6 iphone core-data objective-c ios

我在核心数据方面遇到了一个恼人的问题。我的应用程序需要从 iPhone 获取联系人并将其保存在我的数据库中。我正在尝试在后台线程中执行此操作。

我使用上面的代码:

[self performSelectorInBackground:@selector(fetchingContact) withObject:nil];


    -(void)fetchingContact{
    // Some Code
    for (int i = 0; i < nPeople; i++)
    {
    //Some Code
        NSManagedObjectContext *context = [APP_DELEGATE managedObjectContext];
        ABRecordID recordID = ABRecordGetRecordID(person);
        NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
        NSEntityDescription *entity = [NSEntityDescription entityForName:@"Contact" inManagedObjectContext:context];
        [fetchRequest setEntity:entity];
        NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"(contactId = '%d')",recordID]];
        [fetchRequest setPredicate:predicate];
        NSError *error = nil;
//crash
        NSArray *contactObjArray = [context executeFetchRequest:fetchRequest error:&error];
//crash
        if (error) {}
        Contact *contacts;
        if (contactObjArray.count == 0) {
            contacts = [NSEntityDescription insertNewObjectForEntityForName:@"Contact" inManagedObjectContext:context];
        }else {
            contacts = [contactObjArray objectAtIndex:0];
        }
    //Some Code
    }
    }
Run Code Online (Sandbox Code Playgroud)

在应用程序委托中:

- (NSManagedObjectContext *)managedObjectContext
{
     NSLog(@"managedObjectContext");
    // Returns ;the managed object context for the application (which is already bound to the persistent store coordinator for the application.)
     if (_managedObjectContext != nil) {
          return _managedObjectContext;
     }

     NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
     if (!coordinator) {
          return nil;
     }
     _managedObjectContext =  [[NSManagedObjectContext alloc]initWithConcurrencyType: NSMainQueueConcurrencyType];
     [_managedObjectContext setPersistentStoreCoordinator:coordinator];
     return _managedObjectContext;
}
Run Code Online (Sandbox Code Playgroud)

在这里,我尝试保存在我的中Core Data,但它因错误而崩溃:

* 由于未捕获的异常“NSGenericException”而终止应用程序,原因:“* Collection <__NSCFSet: 0x15fad880> was mutated while being enumerated.” 在这一行:

 NSArray *contactObjArray = [context executeFetchRequest:fetchRequest error:&error];
Run Code Online (Sandbox Code Playgroud)

我已经在网上搜索,找到了很多东西,但没有任何帮助。当我运行它时,没有任何地方可以更改核心数据或联系人实体。那这个错误确实很奇怪。

如果我在主线程中运行它,我不会收到任何错误/不会崩溃,但是如果应用程序在那段时间退出(执行时),我会丢失所有内容Contact

请任何帮助。告诉我是否需要提供更多信息。

Dam*_*rot 8

当您在尝试获取核心数据时修改它们时,会发生此错误。

这也可能是您正在执行的循环的原因,您在 coredata 中插入一个新对象,而在执行其他检索之前没有保存。尝试保存您的 ManagedObjectContext :

我最喜欢的核心数据有一个恼人的问题。我的应用程序需要从 iPhone 获取联系人并将其保存在我的数据库中。我正在尝试在后台线程中执行此操作。

我使用上面的代码:

[self performSelectorInBackground:@selector(fetchingContact) withObject:nil];


-(void)fetchingContact{
// Some Code
for (int i = 0; i < nPeople; i++)
{
//Some Code
    NSManagedObjectContext *context = [APP_DELEGATE managedObjectContext];
    ABRecordID recordID = ABRecordGetRecordID(person);
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Contact" inManagedObjectContext:context];
    [fetchRequest setEntity:entity];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"(contactId = '%d')",recordID]];
    [fetchRequest setPredicate:predicate];
    NSError *error = nil;
    NSArray *contactObjArray = [context executeFetchRequest:fetchRequest error:&error];
    if (error) {}
    Contact *contacts;
    if (contactObjArray.count == 0) {
        contacts = [NSEntityDescription insertNewObjectForEntityForName:@"Contact" inManagedObjectContext:context];
        [context save:&error];
    }else {
        contacts = [contactObjArray objectAtIndex:0];
    }
//Some Code
}
}
Run Code Online (Sandbox Code Playgroud)

如果这不能解决您的问题,也许检查您的方法是否被多次调用。