Core Data - Re-saving Object in didSave

2019-09-10 09:55发布

问题:

I have to check for certain properties after saving the object to the database (I need to make sure first that it's saved on disk). So, I thought that the didSave method of NSManagedObject is the best place to do so.

However, after checking for these properties and changing some of them, I want to re-save the object. So, I call the managed object context to save the object one more time. (I made heavy testing to make sure I won't get into an infinite loop).

Now, the problem is the managed object context doesn't perform the second save. How did I know that? Well, first I checked the hasChanged property of the context for the second save and it returns no. Also, the didSave method is not called one more time due to the re-save.

Is there something I am doing wrong? What's wrong with my algorithm?

Note:

I considered willSave at the beginning but as it turned out, willSave is called before validation. The object may not be saved on disk after all. I need to perform my check and new setting after saving to disk.