Search code examples
iosiphonecore-dataswift2osx-elcapitan

Shipping core data with iOS app


I am developing an dictionary app that has around 10,000 words in it. I am using core data to store all the data and the language that I am using is swift.

All I want is to ship this data with the app so that when the app is downloaded from the app store, it contains all the data.

I have already searched on this and found that it can be done by including SQLite file to the project. But really do not know how to do that and where is the simulator directory in El Capitan.

I am just a beginer in iOS, so kindly someone explain it to me in very simple steps.


Solution

  • This could be quite a lengthy answer. It would be best to explain it as a tutorial. The following link will show you what to do.

    http://www.appcoda.com/core-data-preload-sqlite-database/

    I found the start of the article/tutorial and the end useful for what you are trying to do.

    I recently preloaded data into an app for a Quiz, using a separate Xcode simulator to create the 3 SQLite files needed. This article will show you how to find them, how to "bundle" them into your Xcode project that you want to 'Pre-Load' and will tell you the code to add inside your 'AppDelegate.swift' file and where.

    There are 3 things that will help you avoid problems in completing your project...

    1. Ensure the Entity and Attribute names (in your Data Model (Core Data)) for the data you are creating in the Simulator (separate project) are identical to the ones in the project you want to 'PreLoad'. [ Otherwise the Preloading WONT work ]

    2. The code to add in 'AppDelegate.swift' is designed to direct your App as to where to go for the preLoaded Data (see the attached Tutorial), however, I found I needed to tweek the code to make it work...(noted below)

    3. Nothing is deleted from the 'AppDelegate.swift' file, merely added to and the names of the 3 SQLite files added to it... e.g.

    In 'AppDelegate.swift' in the Core Data stack... you will find...

    lazy var managedObjectModel: NSManagedObjectModel = {
            // The managed object model for the application. This property is not optional. It is a fatal error for the application not to be able to find and load its model.
            let modelURL = NSBundle.mainBundle().URLForResource("EoMQ", withExtension: "momd")!
            return NSManagedObjectModel(contentsOfURL: modelURL)!
        }()
    

    Leave this alone, but direcly underneath this you need to have the following code...

    // ---------------------------------------------------
            // Create the coordinator and store
            let coordinator = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel)
            let url = self.applicationDocumentsDirectory.URLByAppendingPathComponent("Q100cdCoreData.sqlite") // originally = "SingleViewCoreData.sqlite" - then changed to new name... from imported sqlite files
            // ---------------------------------------------------
            // ** Load the Already made DB from Simulator **
            if !NSFileManager.defaultManager().fileExistsAtPath(url.path!) {
    
                let sourceSqliteURLs = [NSBundle.mainBundle().URLForResource("Q100cdCoreData", withExtension: "sqlite")!, NSBundle.mainBundle().URLForResource("Q100cdCoreData", withExtension: "sqlite-wal")!, NSBundle.mainBundle().URLForResource("Q100cdCoreData", withExtension: "sqlite-shm")!]
    
                let destSqliteURLs = [self.applicationDocumentsDirectory.URLByAppendingPathComponent("Q100cdCoreData.sqlite"), self.applicationDocumentsDirectory.URLByAppendingPathComponent("Q100cdCoreData.sqlite-wal"), self.applicationDocumentsDirectory.URLByAppendingPathComponent("Q100cdCoreData.sqlite-shm")]
    
                for index in 0 ..< sourceSqliteURLs.count {
                    do {
                        try NSFileManager.defaultManager().copyItemAtURL(sourceSqliteURLs[index], toURL: destSqliteURLs[index])
                    } catch {
                        print(error)
                    }
                }
            }   
    

    The 3 files I had created in the simulator were called "Q100cdCoreData" but with three different extensions... (a) .sqlite (b) .wal (c) .shm

    But you need to go through the tutorial and understand the process.