你如何压缩iOS上的Realm DB? [英] How do you compact a Realm DB on iOS?

查看:124
本文介绍了你如何压缩iOS上的Realm DB?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想定期在iOS上压缩Realm实例以恢复空间。我认为这个过程是将数据库复制到临时位置,然后将其复制回来并使用新的default.realm文件。

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 default.realm file.

我的问题是 Realm()就像一个单例并且回收对象,所以我无法真正关闭它并告诉它打开新的default.realm文件。

My problem is Realm() acts like a singleton and recycles objects so I can't really close it and tell it to open the new default.realm file.

此处的文档( https://realm.io/docs/ objc / latest / api / Classes / RLMRealm.html )建议我在 autorelease {} 中包装所有Realm()调用,但它不能是这么复杂。

The docs here (https://realm.io/docs/objc/latest/api/Classes/RLMRealm.html) suggest I wrap all the Realm() calls in autorelease { } but it can't be this complicated.

推荐答案

完全拆除所有检索到的模型访问器确实很棘手,但遗憾的是没有其他方法可以关闭领域。

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.

在启动应用程序时,在专用的自动释放池中打开Realm仍然相对容易,将压缩的副本写入不同的路径并替换default.realm文件包含它。

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.

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



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
}

这篇关于你如何压缩iOS上的Realm DB?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆