簡體   English   中英

建立預填充領域數據庫的最佳方法

[英]Best Way to Build a Pre-Filled Realm Database

我的應用(iOS)將內置大量數據(並且不會同步到服務器)。 我知道我可以獲取一個預填充的領域數據庫,並在首次使用該應用程序時將其復制過來 ,但是我不知道的是: 構建該預填充數據庫的最佳方法什么?

應該修改我的應用程序,通過對所有對象和關系進行硬編碼來靜態創建領域數據庫,然后從模擬器中提取數據庫:

 let car1 = Car(color: "red")
 ...
 let car230 = Car(color: "blue")

 ...
 try realm.write{
    realm.add(car1)
    ...
    realm.add(car230)
 }

還是有一個更明智的方法?

如果數據庫將始終保持不變,出於性能考慮,我會選擇預先創建的數據庫選項。 但是,如果使用數據庫加密,則必須進行一些運行時修改。

要實現這一目標,請查看Realm Cocoa轉換器項目 (Swift或Objective-C)。 您可以創建從CSV,XLSX和JSON文件格式導入的領域文件。

他們的例子很清楚:

var filePaths = [String]() // Array of file paths to each CSV file to include
let destinationRealmPath = '' // Path to the folder that will hold this Realm file

// Analyze the files and produce a Realm-compatible schema
let generator =  ImportSchemaGenerator(files: filePaths)
let schema = try! generator.generate()

// Use the schema and files to create the Realm file, and import the data
let dataImporter = CSVDataImporter(files: filePaths)
try! dataImporter.import(toPath: destinationRealmPath, schema: schema)

您可以在他們的測試項目中查看更多示例。

您可以創建一個簡單的應用程序(可能是同一Xcode工作空間中的新目標),僅用於預先填充Realm數據庫,在模擬器上運行它,打開系統上的sim文件夾,然后將Realm文件從此處拖動到主項目中。 然后,在主應用程序啟動時,將文件從捆綁軟件復制到更合適的位置(例如documents文件夾)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM