0

really sorry for this stupid question, but, just developed an app for reading info from database. When, testing from Xcode, can read data from my base. But, once testing the App and putting the sqlite base in the Copy Bundle Ressources, seems the base is not copied to my iPhone. Guess something is wrong in my AppDelegate, but, honestly got no idea.

lazy var managedObjectModel: NSManagedObjectModel = {
        let modelURL = NSBundle.mainBundle().URLForResource("RechercheRatio", withExtension: "momd")!
    return NSManagedObjectModel(contentsOfURL: modelURL)!
}()

lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator? = {
     var coordinator: NSPersistentStoreCoordinator? = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel)

    let url = self.applicationDocumentsDirectory.URLByAppendingPathComponent("RechercheRatio.sqlite")
    var error: NSError? = nil
    var failureReason = "There was an error creating or loading the application's saved data."

    let mOptions = [NSMigratePersistentStoresAutomaticallyOption : true, NSInferMappingModelAutomaticallyOption : true]
    if coordinator!.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: mOptions, error: &error) == nil {
        coordinator = nil
        // Report any error we got.
        var dict = [String: AnyObject]()
        dict[NSLocalizedDescriptionKey] = "Failed to initialize the application's saved data"
        dict[NSLocalizedFailureReasonErrorKey] = failureReason
        dict[NSUnderlyingErrorKey] = error
        error = NSError(domain: "YOUR_ERROR_DOMAIN", code: 9999, userInfo: dict)
        // Replace this with code to handle the error appropriately.
        // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
        NSLog("Unresolved error \(error), \(error!.userInfo)")
        abort()
    }

if any help or ideas, this will be appreciated.

Thank you

fred
  • 45
  • 1
  • 7
  • possible duplicate of [Any way to pre populate core data?](http://stackoverflow.com/questions/2230354/any-way-to-pre-populate-core-data) – David Berry Feb 20 '15 at 21:16
  • In general, the answer is that `NSDocumentsDirectory` is unpopulated by application installation, and `NSBundle.path` is unwritable, so you have to copy your repopulated database from `NSBundle` to `NSDocumentsDirectory`. – David Berry Feb 20 '15 at 21:17
  • Thanks David, but, don't understand where to put it ans how. Dos you have a swift example ? – fred Feb 21 '15 at 17:23
  • The duplicate has a fairly lengthy discussion on it. – David Berry Feb 21 '15 at 19:07

0 Answers0