Mysterious Core Data Faulting Crash - ios

I am experiencing a mysterious Core Data crash via crash reports in my app, that I am having difficulty coming up with a theory for. No reproduction steps, no obvious causes, but it occurs many thousand times. The crash report is pretty vague but it seems to happen when an NSManagedObject is assigned via property to another? Any theories would be appreciated.
*** Terminating app due to uncaught exception 'NSObjectInaccessibleException', reason: 'CoreData could not fulfill a fault for '0x1dc92160 <x-coredata://6903F7F9-C600-4A29-A538-B3337F1D0BED/Profile/p47854>''
Last Exception Backtrace:
1 CoreFoundation __exceptionPreprocess + 163
2 libobjc.A.dylib objc_exception_throw + 30
3 CoreData _PFFaultHandlerLookupRow + 1477
4 CoreData _PF_FulfillDeferredFault + 203
5 CoreData _PF_ManagedObject_WillChangeValueForKeywithSetMutation + 71
6 CoreData -[NSManagedObject willChangeValueForKey:withSetMutation:usingObjects:] + 84
7 CoreData -[NSManagedObject _includeObject:intoPropertyWithKey:andIndex:] + 690
8 CoreData -[NSManagedObject _maintainInverseRelationship:forProperty:oldDestination:newDestination:] + 444
9 CoreData -[NSManagedObject _didChangeValue:forRelationship:named:withInverse:] + 1290
10 CoreData -[NSManagedObjectContext observeValueForKeyPath:ofObject:change:context:] + 322
11 Foundation NSKeyValueNotifyObserver + 273
12 Foundation NSKeyValueDidChange + 335
13 Foundation -[NSObject didChangeValueForKey:] + 94
14 CoreData -[NSManagedObject didChangeValueForKey:] + 126
15 CoreData -[NSManagedObject _updateFromRefreshSnapshot:includingTransients:] + 614
16 CoreData -[NSManagedObjectContext _copyChildObject:toParentObject:fromChildContext:] + 96
17 CoreData -[NSManagedObjectContext _parentProcessSaveRequest:inContext:error:] + 554
18 CoreData __82-[NSManagedObjectContext(_NestedContextSupport) executeRequest:withContext:error:]_block_invoke_0 + 565
19 libdispatch.dylib _dispatch_barrier_sync_f_slow_invoke + 81
20 libdispatch.dylib _dispatch_client_callout + 23
21 libdispatch.dylib _dispatch_main_queue_callback_4CF$VARIANT$up + 227
22 CoreFoundation __CFRunLoopRun + 1291
23 CoreFoundation CFRunLoopRunSpecific + 356
24 CoreFoundation CFRunLoopRunInMode + 104
25 GraphicsServices GSEventRunModal + 74
26 UIKit UIApplicationMain + 1120
27 My App main (main.m:18)
28 libdyld.dylib start + 0

“CoreData could not fulfill a fault” usually happens when you delete some object from the persistent store using one context, but in another context this object still exists, it is a fault, and you try to access some property of it.
Don’t forget that an object can be deleted as a result of cascade deletion rule for a relationship.
Here’s the possible timeline:
The object is fetched in context A. By default it is a fault.
The object representing the same data in the store is fetched in context B.
The object is deleted in context B.
Context B is saved resulting in data deleted from the store.
Some property of the object from context A is accessed.
The fault is being fired. Core Data tries to fulfill the fault, but there’s no data in the persistent store anymore.

If an object that was already fetched has been deleted on another thread or in a different ManagedObjectContext, it is possible that you receive an exception when you try to access a property of the deleted object.
This is due to faulting which is described here:
https://developer.apple.com/library/ios/documentation/cocoa/conceptual/CoreData/Articles/cdFaultingUniquing.html

Related

'-[Page compare:] unrecognized selector sent to instance' in CoreData

EDIT: Page is one of my entities
EIDT2: I'm always running with '-com.apple.CoreData.ConcurrencyDebug 1' and I've got no concurrency issues reported by that
I've stared to see a crash that I have no clue how to solve. I can't find when it was introduced it seems to come from CoreData. And also it's not 100% reproducible.
How can I track it down better? Is it me?
Fatal Exception: NSInvalidArgumentException
-[Page compare:]: unrecognized selector sent to instance 0x281c640a0
With the following stack-trace:
Fatal Exception: NSInvalidArgumentException
0 CoreFoundation 0x192cc896c __exceptionPreprocess
1 libobjc.A.dylib 0x1929e1028 objc_exception_throw
2 CoreFoundation 0x192bc6dcc -[NSOrderedSet initWithSet:copyItems:]
3 CoreFoundation 0x192ccd048 ___forwarding___
4 CoreFoundation 0x192ccf3a0 _CF_forwarding_prep_0
5 Foundation 0x1930afc3c _NSCompareObject
6 CoreData 0x197670e24 +[NSFetchedResultsController(PrivateMethods) _insertIndexForObject:inArray:lowIdx:highIdx:sortDescriptors:]
7 CoreData 0x197671034 -[NSFetchedResultsController(PrivateMethods) _updateFetchedObjectsWithInsertChange:]
8 CoreData 0x19766b73c -[NSFetchedResultsController(PrivateMethods) _updateFetchedObjectsWithInsertedObjects:deletedObjects:updatedObjects:]
9 CoreData 0x19779829c __82-[NSFetchedResultsController(PrivateMethods) _core_managedObjectContextDidChange:]_block_invoke
10 CoreData 0x197757aac developerSubmittedBlockToNSManagedObjectContextPerform
11 CoreData 0x1976221c4 -[NSManagedObjectContext performBlockAndWait:]
12 CoreData 0x19762b81c -[NSFetchedResultsController(PrivateMethods) _core_managedObjectContextDidChange:]
13 Foundation 0x19309306c __57-[NSNotificationCenter addObserver:selector:name:object:]_block_invoke_2
14 CoreFoundation 0x192c2099c __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__
15 CoreFoundation 0x192c209ec ___CFXRegistrationPost1_block_invoke
16 CoreFoundation 0x192c1fce4 _CFXRegistrationPost1
17 CoreFoundation 0x192c1f97c ___CFXNotificationPost_block_invoke
18 CoreFoundation 0x192b98910 -[_CFXNotificationRegistrar find:object:observer:enumerator:]
19 CoreFoundation 0x192c1f2ac _CFXNotificationPost
20 Foundation 0x192f81bfc -[NSNotificationCenter postNotificationName:object:userInfo:]
21 CoreData 0x1976329dc -[NSManagedObjectContext(_NSInternalNotificationHandling) _postContextDidMergeChangesNotificationWithUserInfo:]
22 CoreData 0x19762b6b4 -[NSManagedObjectContext(_NSInternalChangeProcessing) _createAndPostChangeNotification:deletions:updates:refreshes:deferrals:wasMerge:]
23 CoreData 0x197635e00 -[NSManagedObjectContext(_NSInternalChangeProcessing) _postRefreshedObjectsNotificationAndClearList]
24 CoreData 0x19762a4fc -[NSManagedObjectContext(_NSInternalChangeProcessing) _processRecentChanges:]
25 CoreData 0x197631f1c -[NSManagedObjectContext _mergeChangesFromDidSaveDictionary:usingObjectIDs:]
26 CoreData 0x19763228c -[NSManagedObjectContext mergeChangesFromContextDidSaveNotification:]
27 CoreData 0x197757aac developerSubmittedBlockToNSManagedObjectContextPerform
28 libdispatch.dylib 0x19296dfd8 _dispatch_client_callout
29 libdispatch.dylib 0x192979cc8 _dispatch_main_queue_callback_4CF
30 CoreFoundation 0x192c43cc8 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__
31 CoreFoundation 0x192c3ea24 __CFRunLoopRun
32 CoreFoundation 0x192c3df40 CFRunLoopRunSpecific
33 GraphicsServices 0x19cebb534 GSEventRunModal
34 UIKitCore 0x196db6a60 UIApplicationMain
35 Calendarly 0x1005812fc main + 29 (AppDelegate.swift:29)
36 libdyld.dylib 0x192abce18 start
I think I've found it! As a last sorting-resort, I wanted to sort by primary-key and added this:
And it seems like that didn't work.. Because the crash happens when the names and years are the same, thus it's using: NSSortDescriptor(key: "self", ascending: true)
5 Foundation 0x1b8d8bc34 _NSCompareObject + 64 (NSSortDescriptor.m:275) in another crash I got from a the crash, which gave me the file as well (NSSortDescriptor.m), which got me thinking more...
Shortly after I posted the question I got another crash report which had the file-names in it as well (not just functions).
Which was:
5 Foundation 0x1b8d8bc34 _NSCompareObject + 64 (NSSortDescriptor.m:275
This lead me to believe it had something to do with NSSortDescriptor, and surely I added some sorting on self some time ago to keep a consistent order between items that had the same "name" and "year".
Interestingly the crash just occurred when inserting objects and not when loading up the collectionView.
I've actually found the post that led me to do this (though I must have missed the warning):
https://stackoverflow.com/a/52881564/897465

Core data crash when resetting the store: 'Object's persistent store is not reachable from this NSManagedObjectContext's coordinator'

I get a crash when trying to delete the persistent store of Core data in my iOS app written in Swift. The flow is straight forward: When I log out from the app I delete the store with:
destroyPersistentStoreAtURL
I use native Core Data implementation in the application and every access to a managed object is made with performBlock/performBlockAndWait. Also, these operations are in a NSOperationQueue. The flow is the following:
Log out
cancelAllOperations & waitUntilAllOperationsAreFinished on the queue that the performBlocks are implemented
maxConcurrentOperationCount = 1 on the queue of the performBlocks
Finally, I add an operation which destroys the persistent store in the previous NSOperationQueue
Sometimes, I get a crash and I cannot understand why. From what I see, it is something related with managedObjectsIDs and retain. Take a look:
2016-11-14 15:51:58.053 ******[3912:179074] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Object's persistent store is not reachable from this NSManagedObjectContext's coordinator'
*** First throw call stack:
(
0 CoreFoundation 0x000000010ca0f34b __exceptionPreprocess + 171
1 libobjc.A.dylib 0x000000010c05321e objc_exception_throw + 48
2 CoreData 0x000000010c5683b2 _PFRetainedObjectIDCore + 1074
3 CoreData 0x000000010c5507fc -[NSManagedObjectContext objectWithID:] + 668
4 CoreData 0x000000010c590264 _faultBatchAtIndex + 1524
5 CoreData 0x000000010c59217a -[_PFBatchFaultingArray retainedObjectAtIndex:] + 74
6 CoreData 0x000000010c592262 -[_PFBatchFaultingArray objectAtIndex:] + 50
7 CoreData 0x000000010c67d9de __72-[NSFetchedResultsController(PrivateMethods) _computeSectionInfo:error:]_block_invoke + 190
8 CoreData 0x000000010c55adc7 developerSubmittedBlockToNSManagedObjectContextPerform + 199
9 CoreData 0x000000010c55ac7f -[NSManagedObjectContext performBlockAndWait:] + 255
10 CoreData 0x000000010c67d3b6 -[NSFetchedResultsController(PrivateMethods) _computeSectionInfo:error:] + 694
11 CoreData 0x000000010c681d75 __82-[NSFetchedResultsController(PrivateMethods) _core_managedObjectContextDidChange:]_block_invoke + 1077
12 CoreData 0x000000010c55adc7 developerSubmittedBlockToNSManagedObjectContextPerform + 199
13 CoreData 0x000000010c55ac7f -[NSManagedObjectContext performBlockAndWait:] + 255
14 CoreData 0x000000010c681927 -[NSFetchedResultsController(PrivateMethods) _core_managedObjectContextDidChange:] + 119
15 CoreFoundation 0x000000010c9ad19c __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ + 12
16 CoreFoundation 0x000000010c9ad09b _CFXRegistrationPost + 427
17 CoreFoundation 0x000000010c9ace02 ___CFXNotificationPost_block_invoke + 50
18 CoreFoundation 0x000000010c96fea2 -[_CFXNotificationRegistrar find:object:observer:enumerator:] + 2018
19 CoreFoundation 0x000000010c96ef3b _CFXNotificationPost + 667
20 Foundation 0x000000010bb1b0ab -[NSNotificationCenter postNotificationName:object:userInfo:] + 66
21 CoreData 0x000000010c5432b0 -[NSManagedObjectContext(_NSInternalNotificationHandling) _postObjectsDidChangeNotificationWithUserInfo:] + 704
22 CoreData 0x000000010c562b50 -[NSManagedObjectContext(_NSInternalNotificationHandling) _processChangedStoreConfigurationNotification:] + 2976
23 CoreData 0x000000010c5d97ed __95-[NSManagedObjectContext(_NSInternalNotificationHandling) _sendOrEnqueueNotification:selector:]_block_invoke + 109
24 CoreData 0x000000010c55adc7 developerSubmittedBlockToNSManagedObjectContextPerform + 199
25 libdispatch.dylib 0x000000010e0250cd _dispatch_client_callout + 8
26 libdispatch.dylib 0x000000010e0058d6 _dispatch_main_queue_callback_4CF + 406
27 CoreFoundation 0x000000010c9d34f9 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
28 CoreFoundation 0x000000010c998f8d __CFRunLoopRun + 2205
29 CoreFoundation 0x000000010c998494 CFRunLoopRunSpecific + 420
30 GraphicsServices 0x000000010f5d0a6f GSEventRunModal + 161
31 UIKit 0x000000010a050964 UIApplicationMain + 159
32 ****** 0x000000010817e932 main + 114
33 libdyld.dylib 0x000000010e07168d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
Any help is much appreciated!
You need to reset managedObjectContext which was used before this operation.
Call managedObjectContext.reset()
The error is telling you that you're still trying to use a managed object after you have destroyed the persistent store from which it was fetched. That's guaranteed to cause exactly this crash.
It's impossible to say exactly where this is happening, but if you keep any reference to a managed object after removing the store, you'll get this. Cancelling operations, using performBlock, etc, don't make any difference if at the end you're still trying to use a managed object that doesn't have a persistent store any more.
I had the same issue and Tom's hint assuming that dirty objects — that used to live in the old store — where still part of the managed object context after I removed the persistent store from the coordinator. In my case I implemented a "revert to last saved document version" which needs to make sure that these dirty objects are being discarded first.
[_managedObjectContext reset]
[_managedObjectContext.persistentStoreCoordinator removePersistentStore:_store error:outError]
If you still have references to these objects in your code, object.managedObjectContext will be nil — which is a good hint to recover.
#Tom
Yes, you are right. So I post you an example:
let queue = NSOperationQueue
queue.addOperationWithBlock{
let moc = newPrivateQueueManagedObjectContext()
moc.performBlock {
//some work to do on the context
}
and then sometime I destroy the store, but before that I cancel the operations on the above queue. Still, sometimes I see a crash...

Core Data lightweight migration: change relationship from To-One to To-Many

I recently made a few changes to my Core Data model file and I'm trying to perform a lightweight migration. I've already worked with lightweight migrations before so I know I did all the setup correctly.
My only problem is that I changed the type of a relationship: it used to be To-One and now it's To-Many. According to Apple's documentation, lightweight migrations should work just fine in this case, but when I run the code and the system starts migrating the data, there is a crash.
I noticed in the stack trace the following method is causing the crash:
3 CoreData 0x008df2d1 -[_NSSQLEntityMigrationDescription
_populateSQLValuesForDestinationToOne:fromSourceToOne:] + 145
So for some reason the method being called is trying to migrate the relationship as if it still was a To-One relationship.
Why is the mapping model being inferred incorrectly, and how can I fix it?
Thanks!
EDIT
This is the error message that I get:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: '*** setObjectForKey: object cannot be nil (key: Z8CUSCONNECTION)'
cusConnection is the name of the relationship that I changed from To-One to To-Many.
Also, here is the stack trace:
0 CoreFoundation 0x01588746 __exceptionPreprocess + 182
1 libobjc.A.dylib 0x01211a97 objc_exception_throw + 44
2 CoreFoundation 0x01479c9c -[__NSDictionaryM setObject:forKey:] + 940
3 CoreData 0x009182d1 -[_NSSQLEntityMigrationDescription _populateSQLValuesForDestinationToOne:fromSourceToOne:] + 145
4 CoreData 0x00918d30 -[_NSSQLEntityMigrationDescription _populateSQLValuesForVirtualToOnesWithMigrationContext:] + 1648
5 CoreData 0x00918202 -[_NSSQLEntityMigrationDescription _generateSQLValueMappingsWithMigrationContext:] + 962
6 CoreData 0x0091998b -[_NSSQLEntityMigrationDescription sqlValueForColumnName:migrationContext:] + 59
7 CoreData 0x00923cf1 -[_NSSQLTableMigrationDescription createInsertStatementForEntityMigration:migrationContext:] + 689
8 CoreData 0x00922d07 -[_NSSQLTableMigrationDescription appendStatementsToPerformMigration:migrationContext:] + 1143
9 CoreData 0x0091d024 -[_NSSQLiteStoreMigrator createEntityMigrationStatements] + 900
10 CoreData 0x0091b088 -[_NSSQLiteStoreMigrator performMigration:] + 104
11 CoreData 0x0092753a -[NSSQLiteInPlaceMigrationManager migrateStoreFromURL:type:options:withMappingModel:toDestinationURL:destinationType:destinationOptions:error:] + 1962
12 CoreData 0x008b9104 -[NSMigrationManager migrateStoreFromURL:type:options:withMappingModel:toDestinationURL:destinationType:destinationOptions:error:] + 692
13 CoreData 0x00908605 -[NSStoreMigrationPolicy(InternalMethods) migrateStoreAtURL:toURL:storeType:options:withManager:error:] + 341
14 CoreData 0x009074b5 -[NSStoreMigrationPolicy migrateStoreAtURL:withManager:metadata:options:error:] + 117
15 CoreData 0x00909380 -[NSStoreMigrationPolicy(InternalMethods) _gatherDataAndPerformMigration:] + 3136
16 CoreData 0x008c2c76 __91-[NSPersistentStoreCoordinator addPersistentStoreWithType:configuration:URL:options:error:]_block_invoke + 5270
17 CoreData 0x008d12ff gutsOfBlockToNSPersistentStoreCoordinatorPerform + 191
18 libdispatch.dylib 0x03593bef _dispatch_client_callout + 14
19 libdispatch.dylib 0x03576b0d _dispatch_barrier_sync_f_invoke + 144
20 libdispatch.dylib 0x0357623f dispatch_barrier_sync_f + 105
21 CoreData 0x008c03f7 _perform + 183
22 CoreData 0x007b272c -[NSPersistentStoreCoordinator addPersistentStoreWithType:configuration:URL:options:error:] + 236
23 Nimbus II 0x00117fd2 -[AppDelegate persistentStoreCoordinator] + 674
24 Nimbus II 0x002c79b6 +[UserInformationParser saveData:] + 374
25 Nimbus II 0x001c2f2e __47-[SynchronizationController synchronizeData]_block_invoke + 366
26 libdispatch.dylib 0x035715ea _dispatch_call_block_and_release + 15
27 libdispatch.dylib 0x03593bef _dispatch_client_callout + 14
28 libdispatch.dylib 0x0357b1ef _dispatch_root_queue_drain + 1092
29 libdispatch.dylib 0x0357cb70 _dispatch_worker_thread3 + 115
30 libsystem_pthread.dylib 0x038d843e _pthread_wqthread + 1050
31 libsystem_pthread.dylib 0x038d5f72 start_wqthread + 34
Lightweight (or Automatic) migration does support changing relationship from To-one to To-many.
Several steps:
Create new model (xcdatamodel)
Reset the type if you've created a class (from a class type to NSSet since its one-to-many relationship)
Change the relationship for the entity in the new model you just created (from one-to-one to one-to-many)
In the Data Model Inspector of the relationship, under the Versioning, set the Renaming ID with the last relationship name (without s).
The lightweight migrator will create mapping based on it.
It works as same as renaming the entity, etc.

CoreData could not fulfill a fault

I have a problem with the CoreData and fulfilling managedObjects.
Stack trace:
Uncaught exception: CoreData could not fulfill a fault for '0x11cec410 <x-coredata://08BF0B39-BA5D-404E-B75E-FD4FA906DE3E/TaskDescription/p1747>'. Stack trace: (
"4 libc++abi.dylib 0x03553f60 _ZSt11__terminatePFvvE + 14",
"5 libc++abi.dylib 0x03553b97 __cxa_rethrow + 103",
"6 libobjc.A.dylib 0x02cb0a57 objc_exception_rethrow + 47",
"7 CoreFoundation 0x02f95bc5 CFRunLoopRunSpecific + 613",
"8 CoreFoundation 0x02f9594b CFRunLoopRunInMode + 123"
)
*** Terminating app due to uncaught exception 'NSObjectInaccessibleException', reason: 'CoreData could not fulfill a fault for '0x11cec410 <x-coredata://08BF0B39-BA5D-404E-B75E-FD4FA906DE3E/TaskDescription/p1747>''
*** First throw call stack:
(
0 CoreFoundation 0x02ff05e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x02cb08b6 objc_exception_throw + 44
2 CoreData 0x0298666b _PFFaultHandlerLookupRow + 2715
3 CoreData 0x02985bc7 -[NSFaultHandler fulfillFault:withContext:forIndex:] + 39
4 CoreData 0x029857a3 _PF_FulfillDeferredFault + 259
5 CoreData 0x029855f6 _sharedIMPL_pvfk_core + 70
6 CoreData 0x0298a005 -[NSManagedObject(_PFDynamicAccessorsAndPropertySupport) _genericValueForKey:withIndex:flags:] + 85
7 CoreData 0x029c3ab1 _PF_Handler_Public_GetProperty + 161
8 CoreData 0x029c39b5 -[NSManagedObject valueForKey:] + 149
9 Foundation 0x0254e065 -[NSFunctionExpression expressionValueWithObject:context:] + 1260
10 Foundation 0x025d4109 -[NSComparisonPredicate evaluateWithObject:substitutionVariables:] + 248
11 Foundation 0x025d4009 -[NSPredicate evaluateWithObject:] + 48
12 CoreData 0x02a8f991 -[NSFetchedResultsController(PrivateMethods) _objectInResults:] + 113
13 CoreData 0x02a912c2 -[NSFetchedResultsController(PrivateMethods) _preprocessUpdatedObjects:insertsInfo:deletesInfo:updatesInfo:sectionsWithDeletes:newSectionNames:treatAsRefreshes:] + 642
14 CoreData 0x02a926f4 -[NSFetchedResultsController(PrivateMethods) _managedObjectContextDidChange:] + 1892
15 Foundation 0x02622e39 __57-[NSNotificationCenter addObserver:selector:name:object:]_block_invoke + 40
16 CoreFoundation 0x0304c524 __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ + 20
17 CoreFoundation 0x02fa407b _CFXNotificationPost + 2859
18 Foundation 0x0255cb91 -[NSNotificationCenter postNotificationName:object:userInfo:] + 98
19 CoreData 0x029974a3 -[NSManagedObjectContext(_NSInternalNotificationHandling) _postObjectsDidChangeNotificationWithUserInfo:] + 83
20 CoreData 0x02a36abf -[NSManagedObjectContext(_NSInternalChangeProcessing) _createAndPostChangeNotification:withDeletions:withUpdates:withRefreshes:] + 367
21 CoreData 0x02993168 -[NSManagedObjectContext(_NSInternalChangeProcessing) _postRefreshedObjectsNotificationAndClearList] + 136
22 CoreData 0x02992d14 -[NSManagedObjectContext(_NSInternalChangeProcessing) _processRecentChanges:] + 3140
23 CoreData 0x029920c9 -[NSManagedObjectContext processPendingChanges] + 41
24 CoreData 0x02966311 _performRunLoopAction + 321
25 CoreFoundation 0x02fb853e __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 30
26 CoreFoundation 0x02fb848f __CFRunLoopDoObservers + 399
27 CoreFoundation 0x02f963b4 __CFRunLoopRun + 1076
28 CoreFoundation 0x02f95b33 CFRunLoopRunSpecific + 467
29 CoreFoundation 0x02f9594b CFRunLoopRunInMode + 123
30 GraphicsServices 0x0488d9d7 GSEventRunModal + 192
31 GraphicsServices 0x0488d7fe GSEventRun + 104
32 UIKit 0x016c294b UIApplicationMain + 1225
33 MYAPP 0x0003bb9d main + 141
34 MYAPP 0x00003055 start + 53
)
libc++abi.dylib: terminating with uncaught exception of type _NSCoreDataException
All I can see when app crashes is that trace with only showing some internal CoreData/Foundation instructions. Can anyone please explain me, what is going on with that
NSFetchedResultsController(PrivateMethods)
or
NSManagedObjectContext(_NSInternalChangeProcessing)
It looks very weird to me. How can I prevent from such NSObjectInaccessibleException?
I know that this error is due to previously deleted object. I have many background contexts run asynchronously, but I think that I am keeping the rules given by Apple documentation about concurrency with CoreData. It crashes only sometimes and I can't see anything from this point. Anyone had similar situation?
This has nothing to do with the methods you mentioned.
Nor can you avoid this issue in a multi-context environment where you have more then one child context with the same parent (nil parent included).
This might happen in a multi-context environment (or some unique cases) where one context hold a fault for some object (which he consider to exist) but in the mean time another context delete the object from the store and not notifying the other context of the deletion (before the context holding the fault attempt to fulfil it.
Using a FRC in a multi-context environment ,which does not use a parent child architecture where the FRC context is the parent for all child contexts will always be vulnerable to this exception.
See HERE for some more usecases.
A possible solution would be to delay your actual deletion of FRC tracked objects:
first mark them for deletion (your FRC predicate should take this new property into account)
Then use a background operation to delete objects that were marked for deletion (every given time interval)
A partial solution would be to use a parent child context architecture funnelling all events through the context the FRC is observing.
This is a partial solution due to the fact that this move the problem one level up to the child contexts that are unaware of each other deletions (if child contexts never deal with the same objects in parallel then you might avoid this issue).

NSFetchedResultsController issue on iOS4

I am developing an application which uses CoreData. The view contains a UITableView (containing around 50 elements) which uses a NSFetchedResultsController. There is also a "Pull to refresh" which permits to refresh the stored data after a WS call (in another thread with a new managed object context).
Everything is working fine on iOS5: the database and the tableview are refreshed when I perform a "Pull to refresh". However there is a problem with iOS4. The first "performFetch" works when the application is launched (the tableview contains all database records) but I've got the following error when I perform a "Pull to refresh":
2012-02-29 11:56:09.119 Nanopost[1996:207] *** Terminating app due to uncaught exception 'NSObjectInaccessibleException', reason: 'CoreData could not fulfill a fault for '0x5c3c760 <x-coredata://E176B0A1-275B-4332-9231-49FD88238C2B/Ads/p231>''
*** Call stack at first throw:
(
0 CoreFoundation 0x02bfe919 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x02e595de objc_exception_throw + 47
2 CoreData 0x028b833f _PFFaultHandlerLookupRow + 1407
3 CoreData 0x028b5ee3 _PF_FulfillDeferredFault + 499
4 CoreData 0x028b9f3f _sharedIMPL_pvfk_core + 95
5 CoreData 0x0292a010 _PF_Handler_Public_GetProperty + 160
6 Foundation 0x02442c4f -[NSSortDescriptor compareObject:toObject:] + 128
7 CoreData 0x0297db5e +[NSFetchedResultsController(PrivateMethods) _insertIndexForObject:inArray:lowIdx:highIdx:sortDescriptors:] + 286
8 CoreData 0x0297e1b2 -[NSFetchedResultsController(PrivateMethods) _postprocessInsertedObjects:] + 402
9 CoreData 0x029841bc -[NSFetchedResultsController(PrivateMethods) _managedObjectContextDidChange:] + 1804
10 Foundation 0x02380c1d _nsnote_callback + 145
11 CoreFoundation 0x02bd6cf9 __CFXNotificationPost_old + 745
12 CoreFoundation 0x02b5611a _CFXNotificationPostNotification + 186
13 Foundation 0x023767c2 -[NSNotificationCenter postNotificationName:object:userInfo:] + 134
14 CoreData 0x028c0519 -[NSManagedObjectContext(_NSInternalNotificationHandling) _postObjectsDidChangeNotificationWithUserInfo:] + 89
15 CoreData 0x028f802b -[NSManagedObjectContext mergeChangesFromContextDidSaveNotification:] + 1579
16 Foundation 0x02395e9a __NSThreadPerformPerform + 251
17 CoreFoundation 0x02bdfd7f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
18 CoreFoundation 0x02b3e2cb __CFRunLoopDoSources0 + 571
19 CoreFoundation 0x02b3d7c6 __CFRunLoopRun + 470
20 CoreFoundation 0x02b3d280 CFRunLoopRunSpecific + 208
21 CoreFoundation 0x02b3d1a1 CFRunLoopRunInMode + 97
22 GraphicsServices 0x031e62c8 GSEventRunModal + 217
23 GraphicsServices 0x031e638d GSEventRun + 115
24 UIKit 0x0063cb58 UIApplicationMain + 1160
25 Nanopost 0x0000230a main + 170
26 Nanopost 0x00002255 start + 53
)
terminate called after throwing an instance of '_NSCoreDataException'
When I initialize the "NSFetchRequest", I set the "FetchBatchSize" to 20 (randomly):
[l_FetchRequest setFetchBatchSize:20];
But if I set the "FetchBatchSize" to 25:
[l_FetchRequest setFetchBatchSize:25];
... No more crashes on iOS4 and I don't know why and I want to understand this issue :) I don't think that this line is the real problem. Maybe it indicates another problem somewhere?
Thank you very much in advance for your answers!
Thomas
Is there a reason you are using a different context? I don't switch mine, and I have a pretty similar sounding app.
One thing you might compare to is this very handy CoreDataTableViewController:
http://www.stanford.edu/class/cs193p/cgi-bin/drupal/node/289
I use it and haven't had any issues (though I am not running in iOS 4). You might compare how you hooked up the fetchedResultsController.
One other point - do you have the catch-all breakpoint to push the debugger as soon as an exception is raised? If not, add it (breakpoints, + to add, "Add Exception Breakpoint" and keep defaults). That will put you into the debugger right on the line that raises the exception, which should be useful.
Good luck,
Damien

Resources