域似乎在会话或应用程序升级之间不会保留数据

时间:2019-06-26 00:08:18

标签: ios swift iphone realm realm-mobile-platform

AppDelegate.swift文件中,在函数func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

do {
        let fileURL = try FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false).appendingPathComponent("main-tocca.realm")
        let realmConfig = Realm.Configuration(fileURL: fileURL, readOnly: false, schemaVersion: 4, migrationBlock: { (migration, oldSchemaVersion) in
            NSLog("\(#function) | Migration")
            if oldSchemaVersion < 4 {
                NSLog("\(#function) | Migration to version 4")
                migration.enumerateObjects(ofType: Source.className()) { (_, newSource) in
                    newSource?["fileName"] = ""
                }
            }
        }, deleteRealmIfMigrationNeeded: true, shouldCompactOnLaunch: { (totalBytes, usedBytes) -> Bool in
            // totalBytes refers to the size of the file on disk in bytes (data + free space)
            // usedBytes refers to the number of bytes used by data in the file

            // Compact if the file is over 100MB in size and less than 50% 'used'
            let oneHundredMB = 100 * 1024 * 1024
            return (totalBytes > oneHundredMB) && (Double(usedBytes) / Double(totalBytes)) < 0.5
        })

        Realm.Configuration.defaultConfiguration = realmConfig

        autoreleasepool {
            do {
                // Realm is compacted on the first open if the configuration block conditions were met.
                _ = try Realm(configuration: realmConfig)
            } catch {
                NSLog("\(#function) | Realm Error: \(error.localizedDescription)")
                SentryEventReportManager.report(err: error)
            }
        }
    } catch {
        NSLog("\(#function) | Realm File Error: \(error.localizedDescription)")
        SentryEventReportManager.report(err: error)
    }
}

应用程序在启动后立即执行的第一个操作之一就是访问本地数据库,并查找某些在应用程序的先前使用期间应保留的某些对象。

使用RealmStudio,我可以看到数据库已正确保存,但是在实际设备上使用时,基于应用程序行为,我注意到数据库可能无法正确保存,因此在写入或读取数据库时不会返回错误(所有throws都被正确捕获)

有人报告了类似的问题,答案是重命名default.realm文件(我在做什么),但它似乎仍在发生。

0 个答案:

没有答案