我的目標是在第一次啟動應用程式時預加載Core Data。到目前為止,我運行了一個模擬,并將資料填入Core Data。(我已經勾選了 "允許外部存盤")。
我進入application_support并復制了。MyApp.sqlite-wal, MyApp.sqlite-shm, .MyApp_SUPPORT/_EXTERNAL_DATA/和MyApp.sqlite.
然后我將MyApp.sqlite檔案添加到我的應用程式捆綁包中,并在我的應用程式委托中添加了這段代碼:
lazy var persistentContainer: NSPersistentContainer = {
let modelName = "MyApp"
var container。NSPersistentContainer!
container = NSPersistentContainer(name: modelName)
//預加載
let appName: String = "MyApp"/span>
var persistentStoreDescriptions: NSPersistentStoreDescription
let storeUrl = self.getDocumentsDirectory() .appendingPathComponent("MyApp.sqlite")
if !FileManager.default.fileExists(atPath: (storeUrl.path)) {
let seededDataUrl = Bundle.main.url(forResource: appName, withExtension: "sqlite")
try! FileManager.default.copyItem(at: seededDataUrl! , to: storeUrl)
}
let description = NSPersistentStoreDescription()
description.shouldInferMappingModelAutomatically = true ()
description.shouldMigrateStoreAutomatically = true
description.url = storeUrl
container.persistentStoreDescriptions = [description] 。
/End Preloading
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = erroras NSError? {
fatalError("Unresolved error (error), (error.userInfo)")
}
})
return容器
}()
它可以作業,但看起來它沒有找到保存在外部存盤中的圖片。它們在.MyApp_SUPPORT/_EXTERNAL_DATA中作為參考存在。 我應該在哪里添加這些參考?
加載所有內容是我的目標。
加載一切是我的目標。
uj5u.com熱心網友回復:
如果你有一個名為MyApp.sqlite的檔案,并在某個目錄(這里是應用程式捆綁包)中有外部二進制存盤,Core Data將這些檔案放在一個名為.MyApp_SUPPORT/_EXTERNAL_DATA/的子目錄里。您需要遞回地復制該目錄及其子目錄中的所有檔案。
不過,使用該路徑并不是一個好主意,因為它沒有記錄,而且可能在沒有警告的情況下發生變化。此外,這將錯過 MyApp.sqlite-wal 和 MyApp.sqlite-shm,如果它們存在的話。
一個更好的主意是將種子存盤放在它自己的自定義目錄中,并從該目錄中復制所有內容。而不是只有MyApp.sqlite,你會有一個名為MyAppSeedData的目錄,它將包含MyApp.sqlite。它還將包含所有其他Core Data需要的東西,如外部二進制檔案。使用相同的FileManager函式來復制MyAppSeedData目錄(因為它將遞回地復制每個檔案),你應該很好。
復制檔案夾的代碼應該是這樣的:
if let sourceDirURL = Bundle. main.url(forResource: "Source Folder"/span>, withExtension: nil) {
let destinationDirURL = FileManager.default.urls(for: . documentDirectory, in: .userDomainMask)。) first! .appendingPathComponent("TestFolder")
if !FileManager.default。 fileExists(atPath: destinationDirURL.path) {
do {
try FileManager.default。 copyItem(at: sourceDirURL, to: destinationDirURL)
} catch {
print("復制目錄錯誤。(錯誤)")
}
}
}
然后你可以在destinationDirURL的末尾添加SQLite檔案名,并將其用于Core Data。
uj5u.com熱心網友回復:
第1步:創建 "MyAppSeedData "目錄并將MyApp.sqlite、MyApp_SUPPORT、MyApp.sqilte-smh、MyApp.sqilte-wal檔案粘貼在里面。
第2步:將MyAppSeedData拖到AppDelegate下的bundle,并勾選添加目標。
第3步:這些函式必須在AppDelegate檔案中:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool
{
/If first launch condition == true {
seedData()
//}
return true
}
func seedData() {
let fm = FileManager.default
/Destination URL: 應用程式檔案夾。
let libURL = fm.urls(for: .libraryDirectory, in: .userDomainMask).first!
let destFolder = libURL.appendingPathComponent("應用支持").path
/Or
//let l1 = NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true).last!
//
///span>開始的URL。MyAppSeedData dir
let folderPath = Bundle.main.resourceURL!.appendingPathComponent("MyAppSeedData").path
let fileManager = FileManager.default
let urls = fileManager.urls(for: .applicationSupportDirectory, in: .userDomainMask)
if let applicationSupportURL = urls.last {
do{
try fileManager.createDirectory(at: applicationSupportURL, withIntermediateDirectories: true, attributes: nil)
}
catch{
print(error)
}
}
copyFiles(pathFromBundle: folderPath, pathDestDocs: destFolder)
}
func copyFiles(pathFromBundle : String, pathDestDocs: String) {
let fm = FileManager.default
do {
let filelist = try fm.contentsOfDirectory(atPath: pathFromBundle)
let fileDestList = try fm.contentsOfDirectory(atPath: pathDestDocs)
for filename in fileDestList {
try FileManager.default.removeItem(atPath: "(pathDestDocs)/(filename)"/span>)
}
for filename in filelist {
try? fm.copyItem(atPath: "(pathFromBundle)/(filename)", toPath: "(pathDestDocs)/(filename)")
}
} catch {
print("錯誤資訊。(錯誤)")
}
}
//MARK: - 核心資料堆疊。
lazy var persistentContainer: NSPersistentContainer = {
let modelName = "MyApp"
var container。NSPersistentContainer!
container = NSPersistentContainer(name: modelName)
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = erroras NSError? {
fatalError("Unresolved error (error), (error.userInfo)")
}
})
return容器
}()
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/307586.html
標籤:
