1

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. 
Junyong Yap
  • 25
  • 1
  • 5
  • I am not sure I understand the question entirely but a local realm and a sync'd realm are two very different things. The process to 'connect' is different and the way in which the files are stored locally are also different. For example, in local only there's a default realm storing your data. When you move to a sync setup, you need to ensure all of your objects have a *partitionKey* and then what happens is each partitionKey is stored in it's own Realm. Technically, all of the objects could have the same partition by the way. – Jay Jul 10 '21 at 15:03
  • Also, you'll need to read the guide on [Realm Sync](https://docs.mongodb.com/realm/sdk/ios/quick-start-with-sync/) as it requires authentication as well as connecting asynchronously with `Realm.asyncOpen(configuration: configuration)`. We may be able to help if the question was clarified - what is the specific issue? That code wont work for Sync'ing - do you have that code which isn't working? – Jay Jul 10 '21 at 15:04
  • All data models do have “@objc dynamic var partitionKey:String” and I’m using registered user ‘._id’ as partitionKey when create data. I will read the realm sync file again to understand more about it. My goal is to allow user to perform sync once they subscribed whereby free user can only register account and write data to default.realm. Problem arise when I want to start syncing the offline data to cloud when they subscribed and I think I need more guides. – Junyong Yap Jul 10 '21 at 15:38
  • I will retest again with the realm.Configuration and see what I can ask for your better understanding. – Junyong Yap Jul 10 '21 at 15:39

0 Answers0