คัดลอกไฟล์ฐานข้อมูล Realm ที่มีอยู่ไปยังตำแหน่ง App Group สำหรับส่วนขยายวันนี้
แอพ iOS ของฉันใช้ฐานข้อมูล Realm ฉันต้องการคัดลอกหรือย้ายไฟล์ฐานข้อมูล default.realm ปัจจุบันไปยังไดเร็กทอรีใหม่ (ตำแหน่ง App Group) เพื่อให้ฉันสามารถแชร์กับวิดเจ็ต Today Extension ได้
ฉันพยายามตามที่โพสต์นี้บอก ( วิธีการถ่ายโอนฐานข้อมูล Realm ไปยัง appgroups )
รหัสหลักคือ
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)")
}
 
    และฉันใส่รหัสนี้ไว้ในขอบเขตการย้าย Realm ภายในdidFinishLaunchingWithOptionsด้านล่างนี้เพื่อให้คุณเห็นภาพที่ชัดเจนขึ้นว่าฉันใช้รหัสนี้ที่ไหน
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)")
                }
            }
    }
    )
}
 
    เมื่อฉันลองสิ่งนี้คอนโซลของฉันแจ้งว่าไม่สามารถบันทึกไฟล์ได้
Error Domain = NSCocoaErrorDomain Code = 512 "ไม่สามารถบันทึกไฟล์“ default.realm” ในโฟลเดอร์“ 2EEADCEE-F9D9-44A8-BDED-B60A689656A2” UserInfo = {NSFileOriginalItemLocationKey = file: /// Users / jm / Library / Developer / CoreSimulator / Devices / 38334AE3-6648-402E-AC18-8252426002D6 / data / Containers / Shared / AppGroup / 2EEADCEE-F9D9-44A8-BDED-B60A265 / default.realm, ......
ฉันได้ยินว่าควรทำสำเนา / ย้ายไฟล์ฐานข้อมูล Realm ก่อนเปิดไฟล์ข้อผิดพลาดนี้เกี่ยวข้องกับสิ่งนั้นหรือไม่
ขอขอบคุณสำหรับความช่วยเหลือของคุณและขอให้เป็นวันที่ดี
คำตอบ
ขอบคุณ @Jay สำหรับการแสดงความคิดเห็นซึ่งช่วยให้ฉันหาทางได้
ดังที่เขากล่าวถึงการย้ายไฟล์ Realm DB ควรทำก่อนที่เราจะเรียกสิ่งที่เกี่ยวข้องกับ Realm
เดิมทีฉันมีรหัสอยู่ในการโยกย้าย Realm ดังนั้นมันจะทำงานเพียงครั้งเดียวเมื่อเวอร์ชันสคีมาเก่ากว่า
แต่ฉันย้ายมันออกไปด้านบนของ didFinishLaunchingWithOptions ดังนั้นตอนนี้มันจึงย้ายไฟล์ Realm DB ไปยังไดเร็กทอรีอื่น
หวังว่านี่จะช่วยใครบางคนที่กำลังดิ้นรน
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()