Primary Data Warehouse Included in the Bundle App

I cannot find a clear description of these steps in Apple docs ...

  • I have xcdatamodeld in my xcode project
  • During startup, my application parses XML (project resource) to populate the main data store (SQLLite)
  • During the life of my application, I add, delete, update the data of this store

Now I want to stop this heavy XML parsing process on the device and directly enable the repository containing the required data.

I have some questions regarding this:

  • Can I populate a repository using an OS X application and then include that repository in an Xcode-iOs project?
  • My store does not appear in Xcode. In fact, it is created at runtime. How to add storage to a project and link it to my xcdatamodeld?
  • I read that this will not allow my store to be writable ... I think I need to copy it to the right place at launch (the basic tutorial on using Core Data is a great help for this). I'm right?

Thanks for your hints. The url or other SO questions will really be appreciated!

Kheraud

+7
source share
3 answers

You can add a storage file to the application (sqlite db in most cases). Then in your delegate deletion edit persistentStoreCoordinator getter merhod:

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator { if (persistentStoreCoordinator_ != nil) { return persistentStoreCoordinator_; } NSString *storePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"CoreDataStore.sqlite"]; // Check if the store exists in. if (![[NSFileManager defaultManager] fileExistsAtPath:storePath]) { // copy the payload to the store location. NSString *bundleStore = [[NSBundle mainBundle] pathForResource:@"YourPayload" ofType:@"sqlite"]; NSError *error = nil; [[NSFileManager defaultManager] copyItemAtPath:bundleStore toPath:storePath error:&error]; if (error){ NSLog(@"Error copying payload: %@", error); } } NSError *error = nil; NSURL *storeURL = [NSURL fileURLWithPath:storePath]; persistentStoreCoordinator_ = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]]; if (![persistentStoreCoordinator_ addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) { NSLog(@"Unresolved error %@, %@", error, [error userInfo]); abort(); } return persistentStoreCoordinator_; } 
+14
source
  • Write a utility application using the data model and classes for the application. Use the utility application to create persistent storage from the data provided by XML.
  • Add a storage file to the application package, like any other resource
  • Select a location in the application directory in which you want the active store to be located, for example. library directory.
  • At startup, the application checks for the presence of storage in the directory. If this is not the case, the application should copy the repository from the application package to the directory using standard NSFileManger methods, like any other file. (Normally you only need to do this when you first create a store.)

That’s all it really is.

+10
source

What you are doing right now (filling at first start) is the "recommended" way to fill up the master data store. Although it's a bit hacky, you could, however, sow the database on the device as follows:

  • Run the application in the simulator
  • Do everything you need for the simulator application to fill up the master data repository.
  • Stop the simulator application
  • Go to the folder with the simulated documents (something like ~/Library/Application Support/iPhone Simulator/4.3/Applications/335567A0-760D-48AF-BC05-7F0D9BD085B6/<app-name>.app/ )
  • Find the sqlite database (it has the name that you provided when initializing Core Data)
  • Copy this database to your project and add it to copy as a resource
  • Add the code to your application:didFinishLaunchingWithOptions: method so that when you first start it will copy the database from the read-only resource directory to the application document directory. Of course, you need to do before initializing Core Data.

Depending on what you store in your database, you can still find problems with a lot of problems or other incompatibilities. To make this approach safer, you can reset the simulator database ( splite3 databasefile .dump >dumpfile ) on your Mac and then include the dump file in your project (as mentioned above) and dump the dump in your application the first time you start (reading the line - in-line and transfer of sql statements to sqlite API).

+6
source

All Articles