I'm using core data and have found the app sometimes crashing after resuming from background. I've identified the crash occurring inside a block method body when I try to access a property on a NSManagedObject
subclass.
I have a property which holds a reference to a NSManagedObject
subclass.
@property(nonatomic,strong) CalItem *calObject;
To reproduce the crash I first need to call the child viewController(NoteViewController
) passing a block (NoteTextBlock
).
NoteViewController *noteViewController = [[NoteViewController alloc]initWithNote:self.calObject.note NoteTextBlock:^(NSString *noteText) {
self.calObject.note = noteText; //crashing here
}];
Then send the app to background and resume it. Afterwards in the NoteViewController I'll return a message to the calling viewController.
if (self.noteTextBlock)
{
self.noteTextBlock(trimmedString);
}
When the block returns and the line self.calObject.note = noteText
gets executed the app crashes.
So apparently you can't put a block on the stack, quite and resume the app and then continue with what was defined inside the block ? Or am I just doing something wrong here ?
Edit:*** Terminating app due to uncaught exception 'NSObjectInaccessibleException', reason: 'CoreData could not fulfill a fault for '0xb253100 <x-coredata://C2304B7C-7D51-4453-9993-D33B9113A7A5/DTODay/p57>''
The block is defined like this inside the child viewController:
@property(nonatomic, copy)NoteTextBlock noteTextBlock;
Edit2
This is what I get when I set a breakpoint on the line where it crashes.(lldb) po self.calObject
$2 = 0x0b4464d0 <DTODay: 0xb4464d0> (entity: DTODay; id: 0xb489d00 <x-coredata://C2304B7C-7D51-4453-9993-D33B9113A7A5/DTODay/p57> ; data: <fault>)
I'm using the MagicalRecord lib to manage all the Core Data stuff.
- (void)applicationDidBecomeActive:(UIApplication *)application
{
if ([NSManagedObjectContext MR_defaultContext] == nil
|| [NSManagedObjectModel MR_defaultManagedObjectModel] == nil
|| [NSPersistentStoreCoordinator MR_defaultStoreCoordinator] == nil
|| [NSPersistentStore MR_defaultPersistentStore] == nil
)
{
//coming back from background, re-init coredata stack
[MagicalRecordHelpers setupCoreDataStackWithAutoMigratingSqliteStoreNamed:DBNAME];
}
I am not familiar with MagicalRecords, but ...
This exception is raised when you have an un-faulted (as can be seen in Edit2) object that no longer (or never had) exists in the store.
This might happen in a few cases:
objectWithID:
There might be other cases i'm forgetting, or unaware of.
if you could describe your stack structure, and your object state/origin we might be able to understand the problem better
Try also saving state on backgound and then restoring state on wake up in your AppDelegate.m
- (void)applicationDidEnterBackground:(UIApplication *)application {
/*
Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
*/
[MagicalRecord cleanUp];
}
- (void) applicationDidBecomeActive:(UIApplication *)application {
/*
Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
*/
[MagicalRecord setupCoreDataStackWithStoreNamed:DBNAME];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With