swift – 在Caches目录中复制Realm文件

前端之家收集整理的这篇文章主要介绍了swift – 在Caches目录中复制Realm文件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我的应用程序被Apple拒绝了:“在发布和内容下载时,您的应用程序在用户的iCloud上存储了13.01 MB,这不符合iOS数据存储指南.”

我知道问题是什么.我可以将我的Realm数据库保存在Caches目录而不是Documents目录中吗?

解决方法

您可以使用Realm.Configuration.fileURL更改Realm文件路径.如下:

let cachesDirectoryPath = NSSearchPathForDirectoriesInDomains(.CachesDirectory,.UserDomainMask,true)[0]
let cachesDirectoryURL = NSURL(fileURLWithPath: cachesDirectoryPath)
let fileURL = cachesDirectoryURL.URLByAppendingPathComponent("Default.realm")

let config = Realm.Configuration(fileURL: fileURL)
let realm = try! Realm(configuration: config)

如果您不想在每次实例化Realm时指定fileURL,则可以使用Realm.Configuration.defaultConfiguration.如果将配置对象设置为defaultConfiguration,则Realm()将配置用作默认配置.

Realm.Configuration.defaultConfiguration = config
let realm = Realm()

另见… https://realm.io/docs/swift/latest/#realm-configuration

猜你在找的Xcode相关文章