My app offers some sort of editor functionality for “text articles”. My editor has two modes. When the editor loads, I create a managed object if it hasn’t been passed in through the initializer. When the user cancels a new edit, I delete the object from the context. However, sometimes,I get an error about the object not being inserted into the context, and sometimes I don’t. So, is the following code a good approach to check if a managed object instance was inserted into a context?
if ([[self.workingManagedObjectInstance managedObjectContext] isEqual:self.managedObjectContext]){
}
My theory is that if [self.workingManagedObjectInstance managedObjectContext] is nil, then it hasn’t been inserted and will not be “isEqual“. Is that a valid way to check that we’re not deleting an object that wasn’t inserted yet?
In my case, I realized that I should be saving my context after creating the initial instance of the managed object. Since I still had a reference to it, I was able to delete it later.