对最近在应用和扩展程序之间共享Realm数据感到非常兴奋.该文档详细说明了如何将默认域设置为应用程序组目录,我已经开始工作了.
这就是我所坚持的 - 将旧数据库转移到应用程序组中的新位置的最佳方法是什么?
基于@segiddins评论,我决定使用NSFileManager将旧数据库移动到应用程序组:
let fileManager = NSFileManager.defaultManager()
//Cache original realm path (documents directory)
let originalDefaultRealmPath = RLMRealm.defaultRealmPath()
//Generate new realm path based on app group
let appGroupURL: NSURL = fileManager.containerURLForSecurityApplicationGroupIdentifier("group.AppGroup")!
let realmPath = appGroupURL.path!.stringByAppendingPathComponent("default.realm")
//Moves the realm to the new location if it hasn't been done previously
if (fileManager.fileExistsAtPath(originalDefaultRealmPath) && !fileManager.fileExistsAtPath(realmPath)) {
var error: NSError?
fileManager.moveItemAtPath(originalDefaultRealmPath, toPath: realmPath, error: &error)
if (error != nil) {
println(error)
}
}
//Set the realm path to the new directory
RLMRealm.setDefaultRealmPath(realmPath)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1414 次 |
最近记录: |