My app has a few properties (relationships) not optional in some entities in my Core Data model. I have the save method on the applicationDidEnterBackground
of the App Delegate. Here lies the problem.
Some of my users keep losing data (save error) because when they are inputting data but not yet fill out the not optional property of the entity, a phone call or a push message comes. They pick up the phone or read the message, come back to the app and continue data input. However, my app has a passcode lock that is required every time the app launches and will take the user to the dashboard controller so they cannot resume the data input before the phone call/message.
So there is a managedObject with unfilled NOT Optional property in the managedObjectContext. The users continue fill out more data then close the app thinking the data has been saved. A few hours later or when they kill the app from the dock and reopen the app, all the data entered after the phone call/message are gone with this error:
NSLocalizedDescription = "The operation couldn\U2019t be completed. (Cocoa error 1570.)";
NSValidationErrorKey = propertyName;
How do I prevent this error from happening? I could think of 2 solutions:
1) Make all properties optional but I will have to change the core data model and do data migration. I have never done this and am afraid if the migration fails when it goes live. All the in app purchases are stored in core data.
2) Somehow check for the bad managedObject with unfilled NOT optional property from the context and delete the object before saving. How do I do this?
3) ?
Thanks,