My code is like this
class RealmDatabaseManager{
var realm:Realm!
let container = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: ProjectSettings.group)!
static let shared = RealmDatabaseManager()
init(){
let defaultURL = Realm.Configuration.defaultConfiguration.fileURL
let realmURL = container.appendingPathComponent("default.realm")
//check if there's file in realmurl
let fileManager = FileManager.default
if !fileManager.fileExists(atPath: realmURL.path) {
try? fileManager.copyItem(at: defaultURL!, to: realmURL)
}
// Config init
let config = Realm.Configuration(fileURL: realmURL, schemaVersion: 9,migrationBlock: {migration,oldVersion in
if oldVersion < 3 {
migration.enumerateObjects(ofType: WordRecordRealm.className()) { oldObject, newObject in
if let asset = oldObject!["noteCards"] as? RealmSwift.List<WordNoteRealm>{
newObject!["lookUpTime"] = asset.first?.decodedObject.addTime ?? Date.now.timeIntervalSince1970
}
else{
newObject!["lookUpTime"] = Date.now.timeIntervalSince1970
}
}
if oldVersion < 9{
migration.enumerateObjects(ofType: WordRecordRealm.className()) { oldObject, newObject in
let t = oldObject!["translations"]!
print(t)
newObject!["originalTranslations"] = t
}
}
}
})
Realm.Configuration.defaultConfiguration = config
realm = try! Realm(configuration: config)
}
}