1

I am experiencing an unusual error in my iOS app.

My app downloads a large number of JSON objects which are then inserted into Core Data. Each download takes the following steps:

  1. Open network connection and get JSON object
  2. One main NSManagedObject is created
  3. Any number between 0 and around 20 additional NSManagedObjects are created
  4. For some, a fetch request for existing NAManagedObjects is performed to find existing matching objects, and a relationship is created, or a new object is created if one isn't found.

Here is the relevant code snippet, where song is an instance of an NSManagedObject subclass, and Category is a different NSManagedObject subclass:

for (NSString *category in dict[@"categories"]) {
    NSError *error;
    NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Category"];
    fetchRequest.predicate = [NSPredicate predicateWithFormat:@"name like %@", category];
    NSArray *fetchResults = [_moc executeFetchRequest:fetchRequest error:&error];

    Category *c;

    if (fetchResults.count > 0) {
        c = fetchResults[0];
    } else {
        c = [NSEntityDescription insertNewObjectForEntityForName:@"Category" inManagedObjectContext:_moc];
        c.name = category;
    }
    [song addCategoriesObject:c];
}

This works fine, until after a few hundred download-and-inserts, the app will crash. The number of successful downloads will vary, seemingly at random.

The line causing the exception is NSArray *fetchResults = [_moc executeFetchRequest:fetchRequest error:&error]; and the log output is this line:

2014-11-17 10:05:54.265 MyApp[3211:1774124] Communications error: <OS_xpc_error: <error: 0x19ce62a80> { count = 1, contents =
    "XPCErrorDescription" => <string: 0x19ce62e78> { length = 22, contents = "Connection interrupted" }
}>
2014-11-17 10:06:03.631 MyApp[3211:1773135] *** Terminating app due to uncaught exception 'NSGenericException', reason: '*** Collection <__NSCFSet: 0x174075ac0> was mutated while being enumerated.'
*** First throw call stack:
(0x188111e48 0x198b140e4 0x1881117fc 0x187dc9cf8 0x1000965d8 0x100097de0 0x1003a4e30 0x1003a4df0 0x1003af854 0x1003a8120 0x1003b175c 0x1003b2f18 0x1993352e4 0x199334fa8)
libc++abi.dylib: terminating with uncaught exception of type NSException

My attempts to Google this error have proved fruitless. (Seriously - I've never searched for an error and got zero results:)

Zero Google hits for error

Has anyone else experienced a similar error and can point me in the right direction? For once I am at a complete loss as to where to even begin with my search here.

1 Answer 1

0

OK it looks like the crash and the OS_xpc_error are completely unrelated.

The OS_xpc_error appeared before the debugger hit the exception breakpoint. I didn't realise though that the exception message wasn't printed until after I hit continue in the debugger. The relevant line was about mutating __NSCFSet, and the problem was due to accessing my NSManagedObjectContext on a background thread.

Creating a new background NSManagedObjectContext, per this question, has solved my issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.