Reputation: 743
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,
Upvotes: 0
Views: 1376
Reputation: 5290
In practice, any value on a CoreData object that the user is responsible to populate should be able to be nil. Your business logic should enforce your rules, not CoreData. Only things such as keys or identifiers should be required to be populated to save.
Upvotes: 0
Reputation: 44760
You can actually catch and display the validation errors that occur is Core Data. Here's a sample of how that can be done: https://stackoverflow.com/a/3510918/171933
That way you could already validate the data before your users save (maybe while they enter the data) and display the appropriate message to them.
Upvotes: 2
Reputation: 70946
Since your app doesn't let people pick up where they left off, you could just dispose of the new, unsaved object when you load your passcode view. You must have a reference to the object they're editing-- so delete it, and move on. Just use the managed object context's deleteObject:
method.
It would be a lot nicer if you could restore the previous state when they return to the app. Make your passcode view overlay the editing view maybe, instead of going back to the app's initial view. Then just hide the passcode view after the user enters their code, and the user continues where they were.
For what it's worth, changing properties from mandatory to optional should not require data migration. Not every change does. But that's not the best solution.
Upvotes: 1