对最近在应用和扩展程序之间共享Realm数据感到非常兴奋.该文档详细说明了如何将默认域设置为app组目录,我已经开始工作了.
解决方法
基于@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)