Ios 将现有领域数据库文件复制到应用程序组位置以进行今日扩展

Ios 将现有领域数据库文件复制到应用程序组位置以进行今日扩展,ios,swift,iphone,realm,Ios,Swift,Iphone,Realm,我的iOS应用程序正在使用领域数据库 我想将当前default.realm数据库文件复制或移动到新目录(应用程序组位置),以便与Today Extension widget共享 我试过了,正如这篇文章所说的() 核心代码是 let fileManager = FileManager.default let originalPath = Realm.Configuration.defaultConfiguration.fileURL! let appGroupURL = FileManager.d

我的iOS应用程序正在使用领域数据库 我想将当前default.realm数据库文件复制或移动到新目录(应用程序组位置),以便与Today Extension widget共享

我试过了,正如这篇文章所说的()

核心代码是

let fileManager = FileManager.default
let originalPath = Realm.Configuration.defaultConfiguration.fileURL!
let appGroupURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.com.myApp")!.appendingPathComponent("default.realm")
do{
    try fileManager.replaceItemAt(appGroupURL, withItemAt: originalPath)
}
catch{
    print("Error information: \(error)")
}

我把这段代码放在域迁移范围内,使用选项完成启动,如下所示 为了让你更清楚地了解我在哪里使用这个代码


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    var config = Realm.Configuration(
        schemaVersion: 1,
        migrationBlock: { migration, oldSchemaVersion in
            if (oldSchemaVersion < 1) {
                let fileManager = FileManager.default
                let originalPath = Realm.Configuration.defaultConfiguration.fileURL!
                let appGroupURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.com.myApp")!.appendingPathComponent("default.realm")
                do{
                    try fileManager.replaceItemAt(appGroupURL, withItemAt: originalPath)
                }
                catch{
                    print("Error information: \(error)")
                }
            }
    }
    )
}



func应用程序(application:UIApplication,didFinishLaunchingWithOptions launchOptions:[UIApplication.launchOptions键:任意]?)->Bool{
var config=Realm.Configuration(
阴谋厌恶:1,
migrationBlock:{migration,oldSchemaVersion in
if(oldschemaversation<1){
让fileManager=fileManager.default
让originalPath=Realm.Configuration.defaultConfiguration.fileURL!
让appGroupURL=FileManager.default.containerURL(用于安全应用程序组标识符:“group.com.myApp”)!.appendingPathComponent(“default.realm”)
做{
请尝试fileManager.replaceItemAt(appGroupURL,withItemAt:originalPath)
}
抓住{
打印(“错误信息:\(错误)”)
}
}
}
)
}
当我尝试此操作时,我的控制台显示无法保存该文件

Error Domain=NSCocoaErrorDomain Code=512“无法将文件“default.realm”保存在文件夹“2eadcee-F9D9-44A8-BDED-b60a689652”中。“UserInfo”={NSFileOriginalItemLocationKey=file:///Users/jm/Library/Developer/CoreSimulator/Devices/38334AE3-6648-402E-AC18-8252426002D6/data/Containers/Shared/AppGroup/2EEADCEE-F9D9-44A8-BDED-B60A689656A2/default.realm

我听说在打开文件之前应该复制/移动领域数据库文件,这个错误与此相关吗


谢谢你的帮助,祝你度过愉快的一天

谢谢@Jay的评论,帮助我找到了一条路

正如他提到的,在调用与领域相关的任何内容之前,应该先移动领域数据库文件

最初,我在领域迁移中有一个代码,这样当模式版本较旧时,它只运行一次

但是我把它移到了外部,在didFinishLaunchingWithOptions的顶部,所以现在它可以将RealmDB文件移动到不同的目录

希望这能帮助那些正在挣扎的人


let fileManager = FileManager.default
let originalPath = Realm.Configuration.defaultConfiguration.fileURL!
let appGroupURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.myApp")!.appendingPathComponent("default.realm")
if fileManager.fileExists(atPath: originalPath.absoluteString) {
    do{
        try fileManager.replaceItemAt(appGroupURL, withItemAt: originalPath)
        print("Successfully replaced DB file")
    }
    catch{
        print("Error info: \(error)")
    }
} else {
    print("File is not exist on original path")
}


var config = Realm.Configuration(
)
config.fileURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.myApp")!.appendingPathComponent("default.realm")

// Tell Realm to use this new configuration object for the default Realm
Realm.Configuration.defaultConfiguration = config

// Now that we've told Realm how to handle the schema change, opening the file
// will automatically perform the migration
let realm = try! Realm()


有两件事。1)一旦你的应用程序与领域“对话”,你就不能以任何方式改变、更改或篡改领域。因此,如果有任何对领域(任何类型)的调用这可能是一个沙箱问题,你的应用程序有权访问正在写入的文件夹吗?谢谢@Jay,我是一个初学者,所以我可能错了,但是如果didFinishLaunchingWithOptions在viewDidLoad之前运行,我想这是第一次调用realm。2)我相信eve so as App Group文件夹是与today extension应用程序共享数据的地方?我是否必须做额外的工作才能访问此路径?很抱歉对您的问题没有太大帮助