问题
Hi supportive experts...
I'm writing my first iPad app and have a working beta copy that parses an XML file to create a UIManagedDocument
Core Data DB on app start up. However this takes a long time and I would like to do it once off line and include the initial data directly into my app bundle.
Needless to say I'm hitting a wall trying to get it to work.
I first followed the command line utility approach see here This worked until I imported the .sqlite file and tried to edit the persistentStoreCoordinator
method - I'm using a UIManagedDocument
and so don't have a persistenStoreCoordinator
method in my AppDelegate
- this page quote "In some cases, you do not have to write any code to load a model. If you use a document-based application on Mac OS X, NSPersistentDocument
manages the task of finding and loading your application’s model for you."
I then tried to copy the UIManagedDocument
from a working version of my app in the simulator as per this StackOverFlow question
This seemed to work except that the persistentStore
is empty and hence my table is also. This is exactly the problem asked here but the response given to that question takes me right back into modifying the NSPersistentStoreCoordinator
method that I don't have.
I added a test NSFetchRequest
into my code where I copied the UIManagedDocument
from my app bundle into the documents directory - so I know that my UIManagedDocument
in my app bundle is empty. I have tried dragging and dropping from finder and coping using terminal - the persistentStore file seems to be correctly moved and have size 106 KB but I still get an empty DB in my App.
Any suggestions on how to get this to work?
Do I need to use configurePersistenStoreCoordinatorForURL:ofType:modleConfiguration:StoreOptions:error:
as per the UIManagedDocument
Class Reference - if so how would I do it?
Do I need to add some of the core data methods into my AppDelegate so I can modify my persistenStoreCoordinator
method - if so what else does this method have to do?
Am I better using the .sqlite DB that my command line utlity created or the persistentStore
UIManagedDocument
seems to use?
回答1:
You can access the persistent store coordinator through the parentContext
:
document.managedObjectContext.parentContext.persistentStoreCoordinator
Or, if you just mean you don't have access to the method that builds the persistent store when the core data stack is built, then it looks like this is the hook that's intended for that purpose, (as you mention):
Override
configurePersistentStoreCoordinatorForURL:ofType:modelConfiguration:storeOptions:error:
to customize the loading or creation of a persistent store.
But the the discussion for that method in the documentation doesn't explain what to do very well. Perhaps you could override it and see what objects are and aren't already created in the stack, there maybe an initialised coordinator attached to the parent context for you to configure. The documentation for the similarly named method on NSPersistentDocument
is like this so perhaps UIManagedDocument
is also.?
来源:https://stackoverflow.com/questions/11489308/pre-load-core-data-database-coming-up-black-with-uimanageddocument