I've tried to bundle my pre-populated Realm file with my app. I copied if and add the copy bundle resources area in Build Phase. But every time started the app, I can't pull the data. When I look at the file my app has created on the device, the classes are there they are empty.
Here's how I fetch the objects
var allTrees: Results<trees>?
allTrees = realm.objects(trees.self)
print(allTrees?.count ?? 0) // always zero
My AppDelegate setup
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
application.statusBarStyle = .lightContent
let bundlePath = Bundle.main.path(forResource: "default", ofType: ".realm")
let destPath = Realm.Configuration.defaultConfiguration.fileURL?.path
let fileManager = FileManager.default
if fileManager.fileExists(atPath: destPath!) {
//File exist, do nothing
print(destPath ?? "if loop")
} else {
do {
//Copy file from bundle to Realm default path
try fileManager.copyItem(atPath: bundlePath!, toPath: destPath!)
} catch {
print(error)
}
}
return true
}
My model class
class trees: Object {
@objc dynamic var turkishName = ""
@objc dynamic var latinName = ""
@objc dynamic var seedTpye = true
@objc dynamic var leafType = true
@objc dynamic var botanicProperties = ""
@objc dynamic var spreadingArea = ""
}