How do you compact a Realm DB on iOS?

前端 未结 3 1534
粉色の甜心
粉色の甜心 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:07

    Well.. it appears as though this issue is mostly obsolete. Realm added an automatic compact feature last fall. Realm Docs / compacting-realms. I think the only reason to do it as described by @marius is if you need to control the user experience and compact in the background.

    See this question for more: How to correctly use shouldCompactOnLaunch in RealmSwift

    0 讨论(0)
  • 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
    }
    
    0 讨论(0)
  • 2021-02-08 20:12

    The answer given by @marius has an issue: the open Realm might still reference the deleted file. This means some writes might end up in the old (deleted) file, causing the app to lose data.

    The correct implementation of compactRealm method looks like this (swift 3):

    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)
    }
    

    This issue has been driving me crazy until I found an answer here

    0 讨论(0)
提交回复
热议问题