Help with setting up Realm Cloud


#1

Does any have a project they can share that demonstrates how to set up Realm Cloud with Full synchronization? I have built their demo app on Github, attempted to use the Realm Login Kit, read through the Realm Object server/Cloud docs several times but I am still unable to build a successful prototype.

Here is what I am trying to do. I am building a CRUD like app in Swift that will allow users to register for an account so they have data synchronization across their iOS devices. I would to enable full sync so all their data is being backed up and synced every time there is a change. Maybe this is assumed but I also want the user to have the capability to look at their data when offline.

I have been trying to set this up for days with no success and it is really frustrating. Can someone please explain step by step what is needed to accomplish this? I just want to use Realm Cloud for authentication.


#2

What are your issues? What is your current state?

The cloud sync is same as a local ream, just you need to open a synced realm - that will sync every data up.


#3

Thank you for the reply. I feel like I have made some progress since posting this question. However, I still need some help.

Here is my code to log in a user:

@IBAction func loginBtnAction(_ sender: UIButton) {
        //MARK: Credentials
        let realmCredentials = SyncCredentials.usernamePassword(username: userNameTextField.text!, password: passTextField.text!, register: false)
        
        //Constants.MY_FULL_INSTANCE_ADDRESS_URL is "https://myrealmaddress.cloud.realm.io"
        SyncUser.logIn(with: realmCredentials, server: Constants.MY_FULL_INSTANCE_ADDRESS_URL) { user, error in
            guard let user = user else {
                fatalError(String(describing: error))
            }
        
            DispatchQueue.main.async {
                //Constants.REALM_URL is "realms://\(myrealmaddress.cloud.realm.io)/~/userRealm"
                let config = user.configuration(realmURL: Constants.REALM_URL, fullSynchronization: true)
                let realm = try! Realm(configuration: config)
                
                print("Login successful")
            }
        }
        
        performSegue(withIdentifier: "toCategoryVC", sender: self)
    }```


Does this log in code look correct? What should I do differently?

After logging in the user, the code below is run in ViewDidLoad of the initialVC to load the todo categories on screen. As of right now, it appears to be adding the categories to the Realm Cloud instance however, with this configuration, is it also getting stored on device? Am I missing something?

    override func viewDidLoad() {
        super.viewDidLoad()
        
        tableView.delegate = self
        tableView.dataSource = self
  
        // Create the configuration
        //Constants.REALM_URL is "realms://\(myrealmaddress.cloud.realm.io)/~/userRealm"
        let config = SyncUser.current?.configuration(realmURL: Constants.REALM_URL, fullSynchronization: true)
        guard let unwrappedConfig = config else { return }
        self.realm = try! Realm(configuration: unwrappedConfig)
        
        categories = realm!.objects(Category.self).sorted(byKeyPath: "name", ascending: true)
        
    }

#4
Does this log in code look correct? What should I do differently?

It is technically correct, but it doesnt do what you want. With this, you open a synced realm, but you dont wait to download/sync the data.
Check out this page: https://docs.realm.io/platform/using-synced-realms/opening-a-synced-realm

This is what you need:

SyncUser.logIn(with: credentials, server: serverURL) { user, error in
if let user = user {
Realm.Configuration.defaultConfiguration = user.configuration()
Realm.asyncOpen() { realm in
// …
}
}
}


#5

+1: Dont use try! , always catch it properly
[In prototyping its fine, just dont use in production.]


#6

Can you provide an example for what to place inside the “Realm.asyncOpen” closure? I understand the realm parameter must be provided for a reason, I just don’t know what to do with it.


#7

would the code in this github project be what I am looking for?

https://github.com/jpsim/GH4992/blob/master/GH4992/AppDelegate.swift


#8

There is no realm input parameter.
Realm.asyncOpen() { realm in

The realm here is the synced reaml object. Same as

try! Realm(configuration: unwrappedConfig)

just async, as it needs time to sync all the data.


#9

I think I figured it out. I have to do some more testing but it looks positive. Thank you for your continued help.