我可以删除领域而不是迁移吗?

时间:2017-05-12 23:46:21

标签: ios swift realm

我们正在使用Realm作为缓存,因此我们只想删除Realm并重新启动,如果我们对Realm中的任何模式进行更改。

这是一个好策略吗?有什么需要注意的吗?

以下是一些示例代码:

class RealmHelper {
    static let SCHEMA_NUMBER = 5
    static let SCHEMA_NUMBER_KEY = "Neutum:RealmHelper:SCHEMA_NUMBER"

    /// Realm property. This is a computed property to keep thread-safety,
    static var realm: Realm = {
        let defaults = UserDefaults.standard
        // By default is 0
        let schemaNumber = defaults.integer(forKey: SCHEMA_NUMBER_KEY)

        if schemaNumber < SCHEMA_NUMBER {
            RealmHelper.dangerousDeleteAllRealmData()
            defaults.set(SCHEMA_NUMBER, forKey: SCHEMA_NUMBER_KEY)
        }

        return try! Realm()
    }()

    /// Should only be used in the static intializer. That is, this will cause
    /// issues if there are any active instances of Realm.
    ///
    /// See the documentation: https://realm.io/docs/swift/latest/#deleting-realm-files
    static func dangerousDeleteAllRealmData() {
        let realmURL = Realm.Configuration.defaultConfiguration.fileURL!
        do {
            try FileManager.default.removeItem(at: realmURL)

            var lockURL = realmURL
            lockURL.appendPathComponent("lock")
            try FileManager.default.removeItem(at: lockURL)

            var logAURL = realmURL
            logAURL.appendPathComponent("log_a")
            try FileManager.default.removeItem(at: logAURL)

            var logBURL = realmURL
            logBURL.appendPathComponent("log_b")
            try FileManager.default.removeItem(at: logBURL)

            var noteURL = realmURL
            noteURL.appendPathComponent("note")
            try FileManager.default.removeItem(at: noteURL)
        } catch {
            // Do nothing (apparently can't even log ErrorType)
        }
    }
}

1 个答案:

答案 0 :(得分:3)

我强烈建议您在deleteRealmIfMigrationNeeded中将true设置为Realm.Configuration

文档:https://realm.io/docs/swift/latest/api/Classes/Realm/Configuration.html#/s:vVC10RealmSwift5Realm13Configuration28deleteRealmIfMigrationNeededSb

当我使用Realm作为缓存来防止迁移问题时的扩展:

extension Realm
{
    static var cacheConfig: Realm.Configuration {
        let cacheDirs = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true)
        let cacheURL = URL(fileURLWithPath: cacheDirs.first!)

        return Realm.Configuration(
            fileURL: cacheURL.appendingPathComponent("cache.realm"),
            inMemoryIdentifier: nil,
            syncConfiguration: nil,
            encryptionKey: nil,
            readOnly: false,
            schemaVersion: 0,
            migrationBlock: nil,
            deleteRealmIfMigrationNeeded: true,
            objectTypes: nil
        )
    }

    static var cache: Realm {
        do {
            return try Realm(configuration: Realm.cacheConfig)
        }
        catch let error as NSError {
            ... handle error ...
        }
    }
}

当我想访问缓存时,只需使用Realm.cache...

赞:Realm.cache.objects(Responses.self)