简体   繁体   English

在应用程序生命周期的中间将本地 Realm 转换为同步 Realm(在 Swift 中)

[英]Converting local Realm to synced Realm in the middle of app life cycle (in Swift)

My app will have a paid feature called multi-devices sync.我的应用程序将具有称为多设备同步的付费功能。 I would like to implement the feature with Realm Cloud - Query Based Sync.我想使用 Realm Cloud - 基于查询的同步来实现该功能。

I know how to convert local Realm to synced Realm thanks to this thread .由于这个线程,我知道如何将本地领域转换为同步领域。

But this is based on the scenario that users sync their Realm from the app start - before opening their non-synced local realm.但这是基于用户从应用程序开始同步他们的 Realm 的场景 - 在打开他们的非同步本地 Realm 之前。 That doesn't work for me because my users will start sync when they paid for it.这对我不起作用,因为我的用户会在付费后开始同步。

Therefore, I have to convert their local Realm in the middle of app life cycle and the local Realm is already opened by that time.因此,我必须在应用程序生命周期的中间转换他们的本地 Realm,并且到那时本地 Realm 已经打开。

My issue comes in here.我的问题出现在这里。 When I try to convert local realm to synced realm, app crashes with this message:当我尝试将本地领域转换为同步领域时,应用程序崩溃并显示以下消息:

Realm at path '…' already opened with different read permissions.路径 '...' 处的领域已经以不同的读取权限打开。

I tried to find a way to close local Realm before converting it, but Realm cocoa does not allow me to close a Realm programmatically.我试图在转换之前找到一种关闭本地 Realm 的方法,但是 Realm cocoa 不允许我以编程方式关闭 Realm。

Here's my code converting local Realm to synced Realm.这是我将本地 Realm 转换为同步 Realm 的代码。

func copyLocalRealmToSyncedRealm(user: RLMSyncUser) {

    let localConfig = RLMRealmConfiguration()
    localConfig.fileURL = Realm.Configuration.defaultConfiguration.fileURL
    localConfig.dynamic = true
    localConfig.readOnly = true

    // crashes here
    let localRealm = try! RLMRealm(configuration: localConfig)

    let syncConfig = RLMRealmConfiguration()
    syncConfig.syncConfiguration = RLMSyncConfiguration(user: user,
                                                        realmURL: realmURL,
                                                        isPartial: true,
                                                        urlPrefix: nil,
                                                        stopPolicy: .liveIndefinitely,
                                                        enableSSLValidation: true,
                                                        certificatePath: nil)
    syncConfig.customSchema = localRealm.schema

    let syncRealm = try! RLMRealm(configuration: syncConfig)
    syncRealm.schema = syncConfig.customSchema!
    try! syncRealm.transaction {
        let objectSchema = syncConfig.customSchema!.objectSchema
        for schema in objectSchema {
            let allObjects = localRealm.allObjects(schema.className)
            for i in 0..<allObjects.count {
                let object = allObjects[i]
                RLMCreateObjectInRealmWithValue(syncRealm, schema.className, object, true)
            }
        }
    }
}

Any help will be appreciated.任何帮助将不胜感激。 Thanks.谢谢。

I made a copy of the local realm file and opened the copy with RLMRealmConfiguration.我制作了本地领域文件的副本,并使用 RLMRealmConfiguration 打开了副本。 Afterwards, just delete both files.之后,只需删除这两个文件。 It is not the best solution, but it works这不是最好的解决方案,但它有效

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM