Pre load core data database coming up black with UIManagedDocument
Asked Answered
S

1

1

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?

Seabrooke answered 15/7, 2012 at 4:0 Comment(1)
I think I solved the problem and have written it up here: #8684392Dihydrostreptomycin
H
0

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.?

Hutchinson answered 15/7, 2012 at 7:30 Comment(5)
Thanks for the response. Following your thinking I have looked at the NSPersistentStoreCoordinator that is created automatically with my UIManagedDocument. I'm still digging into what exactly is going on but I found the following iCloud question where the answer seems very relevant: #10140900Seabrooke
Yes, the migrate method seems like it might be useful to you. I was aware that you can't copy a database over directly when using iCloud. I can't remember exactly which one but I think it was one of the WWDC videos (possibly 2011) which described it. If you are thinking about iCloud, the core data and iCloud video from this years WWDC (2012) has a lot of information about setting up stores and coordinators, albeit not with UIManagedDocument, but there might be some enlightening info in their none the less. They definitely talk about having different stores, some just for static data ...Necrology
... which they use to save bandwidth, but some of this may be useful, especially if you can't get what you want out of UIManagedDocument and end up having to revert back to setting the stack up yourself.Necrology
Thanks again for some great support and ideas. I've been digging into creating a NSPersistentStore object from the persistentStore file that I copied from a working app into my project file structure. The problem is that I get an error about not being able to find its StoreType. Looking closely at the designated init documentation it says you have to initializes a stores metadata - which I haven't done. Looking into how you do that I find developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/…... perhaps I need a new approachSeabrooke
The atomic store classes are for creating your own backing store provider. i.e. you want to use something other that SQLite. The example sometimes given is you want to use some kind of custom web service as a backing store for core data. This sounds like you may be heading down the wrong path here.Necrology

© 2022 - 2024 — McMap. All rights reserved.