Atomic read and write transactions with Realm Object Server


#1

Hi,

I’m currently updating my iOS-App with the Realm Cloud and so far the speed and simplicity is awesome! For the backend I’m using Google App Engine running Node.js.

How can I guarantee atomic transactions with the Realm Object Server? For example I would like to check from my client if a user with a unique username already exists. If not I would like to add one to the database.
Do I have to call my backend, check from there for an existing user with the specific username and insert a user if the name not exists? Is the backend in this case copying the complete database to local storage or is it using the Object Server directly? Is it possible to do this from iOS-Client? What ist the best practice for this?

(This is only an example. Another use case would be managing In-App-Purchases)

I think this question is really naive but even after hours of investigating the Docs and Stack Overflow I couldn’t find an answer. I would really appreciate a small hint!

Thank you and best regards

Marcel


#2

@Marcel Realm Sync is atomic and transactional in nature by default - you can see the details here if you wish - https://docs.realm.io/sync/realm-sync-a-primer

If you are just doing User auth - the Realm.Sync.User.login will automatically check if a user with the same username exists and if it does not you can pass the register flag to the API to register a new user.

If it is a different Object - lets say checking if an Appointment exists at a certain time - then yes you would use the Realm Sync APIs to grab the appointment objects and filter the objects for appointments at that timestamp.


#3

Hi Ian,

thanks for the fast reply! I already integrated the authentication process with Firebase Auth and JWT. In the App I’m using a query based sync. From my understanding this is copying the data from the Realm Object Server to a local realm on the device.

Now I want to create a user object for new users to store some more data. Every user should have a unique username (like Instagram). How can i guarantee a unique username?

Here is an example: The device from user A goes offline. He enters “BestName” as username-> I will check the offline (in this case) realm for a match -> cannot find one -> create new User object -> goes online and sync the data with the server. But in the meantime user B already created a User object with “BestName”. Now I have two user object with the same username.

Is it possible to establish a connection directly with the server from my client? For Firebase Firestore there is a transaction ( Firestore Doc Link ). This is connecting directly to the server and reading and updating data. If the device is offline or an error occurs the transaction will throw. Is there an equivalent for Realm?