1

我的应用程序是免费增值类型,用户可以免费使用该应用程序,但仅限离线使用,并且每个数据都存储在default.realm文件中。

一切正常,但当用户想要与其他设备同步时,他们需要订阅提供的订阅。

这给我带来了一个问题,当用户支付订阅费用时,应用程序将执行sync.configurationrealm.Configuration,但是一旦完成,应用程序中显示的所有数据都无法查看,因为它已更改为。

问题:我如何执行上传/请求领域云来同步 default.realm 中的所有数据?

*** 领域配置 ***

class RealmManager {
    static let shared = RealmManager()
    let realmApp = App(id: "xxxxxxxxxx")
    
    lazy private var realmURL: URL = {
        return Realm.Configuration.defaultConfiguration.fileURL!
    }()
    
    lazy private var config:Realm.Configuration = {
        return Realm.Configuration(
        fileURL: self.realmURL,
        inMemoryIdentifier: nil,
        syncConfiguration: nil,
        encryptionKey: nil,
        readOnly: false,
        schemaVersion: 1,
        migrationBlock: nil,
        deleteRealmIfMigrationNeeded: false,
        shouldCompactOnLaunch: { totalByte, usedByte in
            let twentyMB = 20 * 1024 * 1024
            return (totalByte > twentyMB) && (Double(usedByte) / Double(totalByte)) < 0.6
        },
        objectTypes: nil
        )}()
    
    func realm(config:Realm.Configuration? = nil) -> Realm {
        let user = realmApp.currentUser
        var realm = try! Realm()
        
        if ((user?.isLoggedIn) != nil) {
            let partitionValue = user!.id

            if realm.objects(UserModel.self).first != nil
                && realm.objects(UserModel.self).first?.typeID == UserType.Premium.rawValue {
                print("online user with sync config")
                realm = try! Realm(configuration: (user?.configuration(partitionValue: partitionValue))!)
            } else {
                print("online user without sync config")
                realm = try! Realm(configuration: self.config)
            }
        }
        else {
            print("no sync config")
            realm = try! Realm(configuration: self.config)
        }
        return realm
    }
    
}
/// InAppPurchase: User completed transaction, perform sync to realm cloud
let user = RealmManager().realmApp.currentUser
let configuration = user?.configuration(partitionValue: user?.id ?? "")
_ = RealmManager().realm(config: configuration)
                                
This is the error Code i received after perform /// InAppPurchase: User completed transaction, perform sync to realm cloud


Thread 1: Fatal error: 'try!' expression unexpectedly raised an error: 
Error Domain=io.realm Code=8 "Realm file is currently open in another process which cannot share access with this process. 
All processes sharing a single file must be the same architecture. 
For sharing files between the Realm Browser and an iOS simulator, 
this means that you must use a 64-bit simulator. 
4

0 回答 0