我应该如何命名可导入的.sqlite持久存储?

时间:2017-12-26 14:08:32

标签: ios core-data

我正在使用预先将.sqlite数据库文件导入到我的项目中来创建CoreDataStack。

 func seedCoreDataContainerIfFirstLaunch() {
    //1
    let previouslyLaunched = UserDefaults.standard.bool(forKey: "previouslyLaunched")
    if !previouslyLaunched {
        UserDefaults.standard.set(true, forKey: "previouslyLaunched")
        // Default directory where the CoreDataStore will store its files.
        let directory = NSPersistentContainer.defaultDirectoryURL()
        let url = directory.appendingPathComponent(modelName  + ".sqlite")

        //2: Copying the SQLite file
        let seedDatabaseURL = Bundle.main.url(forResource: modelName , withExtension: "sqlite")!
        print(seedDatabaseURL)
        _ = try? FileManager.default.removeItem(at: url)
        do {
            try FileManager.default.copyItem(at: seedDatabaseURL, to: url)

        } catch let nserror as NSError {
            fatalError("Error: \(nserror), \(nserror.localizedDescription)")
        }
        // 3: Copying the SHM file

        let seededSHMURL = Bundle.main.url(forResource: modelName , withExtension: "sqlite-shm")!
        let shmURL = directory.appendingPathComponent(modelName + ".sqlite-shm")
        _ = try? FileManager.default.removeItem(at: shmURL)
        do {
            try FileManager.default.copyItem(at: seededSHMURL, to: shmURL)
        } catch let nserror as NSError {
            fatalError("Error: \(nserror.localizedDescription)")
        }

  // 4: Copying the WAL file
        let seededWALURL = Bundle.main.url(forResource: modelName  , withExtension: "sqlite-wal")
        let walURL = directory.appendingPathComponent(modelName  + ".sqlite-wal")

  _ = try? FileManager.default.removeItem(at: walURL)
  do {
    try FileManager.default.copyItem(at: seededWALURL!, to: walURL)
  } catch let nserror as NSError {
    fatalError("Error: \(nserror.localizedDescription)")
  }

  print("Seeded Core Data")
}
}

我想知道我必须将我的持久存储命名为我的xcdatamodeled文件的名称吗?如果我必须如何为我的secodpersistence商店命名?

1 个答案:

答案 0 :(得分:1)

持久性存储文件可以包含任何合法的文件名。核心数据没有关于文件名的规则。

NSPersistentContainer使用默认文件名,但您可以更改它。使用其persistentStoreDescriptions属性,该属性允许您指定各种详细信息,包括持久性存储文件的URL。