Pan MluvĨí Pan MluvĨí - 2 months ago 21
Swift Question

Realm: Bundling a Realm with an App

I want to ship my app with several databases.

1) I have a random generated ".csv" file converted with Realm Browser to "default.realm".

2) I put it in /project_name/project_name/Resources and drop it inside Xcode to project files.

3) I checked "Copy bundle resources"enter image description here

4)Created

import RealmSwift

class CarItem: Object {
dynamic var id = String()
dynamic var first_name = String()
}


In ViewDidLoad wanted to return results from file

override func viewDidLoad() {
super.viewDidLoad()

let conf = Realm.Configuration(
fileURL: NSBundle.mainBundle().URLForResource("default", withExtension: "realm"),
readOnly: true)

let realm = try! Realm(configuration: conf)
let results = realm.objects(Item.self)
print(results)
}


5) But results are empty (database have 1000 rows all filled with data).... what am i down wrong?

Answer

CarItem.self replace Item.self ?