Introduction to Atlas Device Sync for Android
Rate this announcement
Atlas App Services (Formerly MongoDB Realm )
Atlas Device Sync (Formerly Realm Sync)
This is a beginner-level article, where we introduce you to Atlas Device Sync. As always, we demonstrate its usage by building an Android app using the MVVM architecture.
You have created at least one app using Android Studio.
syncEnabledstate as shown below in the Gradle file (at the module level):
Also, add the
buildTypesin the same file:
You can ignore the value of
App Keyfor now, as it will be covered in a later step.
Copy the App ID and use it to replace
App Keyin the
build.gradlefile, which we added in Step 2.
With this done, MongoDB Atlas and your Android App are connected.
Device Sync is designed to make apps secure by default, by not allowing an unknown user to access data.
We don't have to force a user to sign up for them to become a known user. We can enable anonymous authentication, which is a win-win for everyone.
So let's enable both of them:
Let's quickly recap what we have done so far.
In the Android app:
- Added App Services App ID to the Gradle file.
- Enabled Atlas Device Sync.
- Set up account.
- Created a App Services app.
- Enabled anonymous authentication.
- Enabled sync.
Now, the final piece is to make the necessary modifications to our Android app.
The only code change is to get an instance of the Realm mobile database from the App Services app instance.
- Get a App Services app instance from which the Realm instance can be derived:
- Update the creation of the View Model:
- Update the View Model constructor to accept the App Services app instance:
- Update the
In the above snippet, we are doing two primary things:
- Getting a user instance by signing in anonymously.
- Getting a Realm instance using