magicalpanda / MagicalRecord

Super Awesome Easy Fetching for Core Data!
Other
10.8k stars 1.79k forks source link

NSInvalidArgumentException Parent NSManagedObjectContext must not be nil. #1222

Open fysteven opened 8 years ago

fysteven commented 8 years ago

Fatal Exception: NSInvalidArgumentException Parent NSManagedObjectContext must not be nil.

NSManagedObjectContext+MagicalRecord.m line 68 +[NSManagedObjectContext(MagicalRecord) MR_contextWithParent:]

Thread : Fatal Exception: NSInvalidArgumentException
0  CoreFoundation                 0x2319db8b __exceptionPreprocess
1  libobjc.A.dylib                0x2295adff objc_exception_throw
2  CoreData                       0x24d22cc3 -[NSManagedObjectContext setParentContext:]
3  Branch                         0x124ead +[NSManagedObjectContext(MagicalRecord) MR_contextWithParent:] (NSManagedObjectContext+MagicalRecord.m:68)
4  Branch                         0x12724d +[NSManagedObjectContext(MagicalThreading) MR_contextForCurrentThread] (NSManagedObjectContext+MagicalThreading.m:55)
5  Branch                         0x121953 +[NSManagedObject(MagicalFinders) MR_findFirstByAttribute:withValue:] (NSManagedObject+MagicalFinders.m:116)
6  Branch                         0x28f4f __46-[BRAllShiftsTableViewCell configureWithModel]_block_invoke (BRAllShiftsTableViewCell.m:56)
7  libdispatch.dylib              0x22d2fcbf _dispatch_call_block_and_release
8  libdispatch.dylib              0x22d3b6a1 _dispatch_root_queue_drain
9  libdispatch.dylib              0x22d3b07b _dispatch_worker_thread3
10 libsystem_pthread.dylib        0x22ecee0d _pthread_wqthread
11 libsystem_pthread.dylib        0x22ece9fc start_wqthread``
dmitrykurochka commented 8 years ago

I got the pretty same error in my application. Any suggestions what could it be?

S2V commented 8 years ago

+1

Muesly commented 8 years ago

+1

adrianramming commented 7 years ago

+1

chillok commented 7 years ago

+1 (Mac)

adriga commented 7 years ago

+1

joshuafeldman commented 7 years ago

+1

aleufms commented 7 years ago

+1

nenadvulic commented 7 years ago

+1

bxyfighting commented 7 years ago

+1

joshuafeldman commented 6 years ago

Just wondering if any leads or ideas have been made regarding what might cause this issue.

DamonRao commented 6 years ago

+1 ,anyone have resovled,please share it .

pagsantos commented 6 years ago

any news about this error?

allenhsu commented 5 years ago

Most of the time, it's due to rootSavingContext set to nil in cleanup method when app being terminated (I suppose you're all calling [MagicalRecord cleanUp]; in - (void)applicationWillTerminate:(UIApplication *)application). So when creating a local context in non-main thread, this error happens. You can check the stack of your main thread to confirm if the app is terminating. If so:

  1. Luckily, user won't feel it like a crash, because the app is already being killed, no matter by user or the system.
  2. To address this issue, you can: 2.1 In most of these methods, you can check whether rootSavingContext is nil first, if it's nil, just return nothing, or call the error callback. Then you also need to better handle this nothing or error state. 2.2 If you have some important tasks in background, e.g. saving data, you'd better wait for them done in applicationWillTerminate method. But do remember that, you have at most ~5 seconds to perform tasks in applicationWillTerminate method.