State Save and Restore Strategies with Core Data Objects in UIManagedDocument

I am starting to try to add support for persisting and restoring state to my iOS app that has a Core Data component that I access through UIManagedDocument.

I am starting to add recovery ids to my view controllers and hook up the required functionality (currently empty) in my AppDelegate and controllers.

I have an object that can potentially be referenced by multiple view controllers, so I plan to try and save and restore this in my AppDelegate, and just have the appropriate view controllers to get the object from the AppDelegate. The timing of this can be tricky because the application's didRecodeRestorableState delegate method occurs after all views have already called their own decodeRestorableStateWithCoder methods.

My main problem is that this generic class as well as several ViewControllers all want to save and restore the NSManagedObject properties. I hope I can use the URIRepresentation object to facilitate this, but the problem I am having is my AppDelegate, which will open my UIManagedDocument in my AppDelegate willFinishLaunchingWithOptions method. This is done using the UIManagedDocument openWithCompletionHandler method. Due to streaming of this opening, the document opens successfully after all my views and the application delegate have already tried to restore their saved state. The AppDelegate sends a notification after the document is ready to use, so all my controllers can listen for that notification.

I think I'm just surprised that this is the best or even the only strategy for solving this problem. My objects will need to hold the URIRs of the statements they are restoring, and only once when the document (and it's NSManagedObjectContext) is ready, try to find and set the appropriate NSManagedObjects they have saved. So the restore happens much later than the calls to do the restore, I guess they usually do all their restore work. I am worried if the controller could potentially appear empty for a short period of time while it waits for a document to open and then initializes correctly.

Is there any purpose in blocking and delaying the opening of my document in this case, so yes, the application takes longer to open, but can at least recover more correctly all the data it needs before any views appear. Are there timers to make sure certain methods don't take too long? It would be more correct to show a different view while we are in this state of uncertainty, not really sure how to do this, but this is what you can see in other applications, for example, say that a Facebook application that depends on the network connection.

I can't find a real explanation for this kind of problem in the documentation yet.

Any help is, as always, greatly appreciated! Greetings

+3


source to share


1 answer


In the end, I just fired notifications when my UIManagedDocument finished loading. They were picked up by all controllers that had coredata, manipulating the objects that he wanted to restore. During recovery, I save the encoded URIs, and later, upon receiving this UIManagedDocument notification, I simply decrypted the URIs to their respective managed objects.

The shared object problem I described I solved by encoding and restoring in one place from my appDelegate application and then using another notification for systems to tell them that this shared object is now fully decoded and available for use.



Not ideal and involved creating quite a few method hierarchies to ensure that all objects are decoded correctly, but it works fine.

Unfortunately, I've since gotten into a stumbling block where the UIDataSourceModelAssociation methods call the OS before my UIManagedDocument finishes opening. Unfortunately, this means that I cannot do anything useful. So what I really need to do is delay restoring my application until everything is loaded from the POV CoreData UIManagedDocument. This problem continues ...

+1


source







All Articles