How do you compact a Realm DB on iOS?

前端 未结 3 1535
粉色の甜心
粉色の甜心 2021-02-08 19:46

I\'d like to compact a Realm instance on iOS periodically to recover space. I think the process is to copy the db to a temporary location, then copy it back and use the new defa

3条回答
  •  粉色の甜心
    2021-02-08 20:10

    It can be indeed tricky to completely tear down all retrieved model accessors, but there is unfortunately no other way to close a Realm.

    As you wrote "periodically" every app launch might be often enough, depending on your use case.

    On the launch of your application, it should be still relatively easy to open Realm in a dedicated autoreleasepool, write a compacted copy to a different path and replace your default.realm file with it.

    Swift 2.1

    func compactRealm() {
        let defaultURL = Realm.Configuration.defaultConfiguration.fileURL!
        let defaultParentURL = defaultURL.URLByDeletingLastPathComponent!
        let compactedURL = defaultParentURL.URLByAppendingPathComponent("default-compact.realm")
    
        autoreleasepool {
            let realm = try! Realm()
            realm.writeCopyToPath(compactedURL)
        }
        try! NSFileManager.defaultManager().removeItemAtURL(defaultURL)
        try! NSFileManager.defaultManager().moveItemAtURL(compactedURL, toURL: defaultURL)
    }
    
    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        compactRealm()
    
        // further setup …
    
        return true
    }
    

    Swift 3.0

    func compactRealm() {
        let defaultURL = Realm.Configuration.defaultConfiguration.fileURL!
        let defaultParentURL = defaultURL.deletingLastPathComponent()
        let compactedURL = defaultParentURL.appendingPathComponent("default-compact.realm")
    
        autoreleasepool {
            let realm = try! Realm()
            try! realm.writeCopy(toFile: compactedURL)
        }
        try! FileManager.default.removeItem(at: defaultURL)
        try! FileManager.default.moveItem(at: compactedURL, to: defaultURL)
    }
    
    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        compactRealm()
    
        // further setup …
    
        return true
    }
    

提交回复
热议问题