default.realm data to realm sync/cloud

160 Views Asked by At

My app is a Freemium type, users can use the app for free but for offline usage only and every data is stored in default.realm file.

Everything works perfectly as it should be, but when user want to synchronize with other devices, they are required to subscribe to subscription offered.

Which raise an issue for me, when user paid for subscription, app will perform sync.configuration to realm.Configuration, but once it's done, all data shown in app are not be available to view as it changed to.

Question: how do I perform upload/request realm cloud to sync all the data in default.realm?

*** Realm Configuration ***

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. 
0

There are 0 best solutions below