- Stitch >
- MongoDB Mobile >
- Usage Guides
Build a Mobile App with Sync¶
On this page
Deprecation
MongoDB has deprecated MongoDB Mobile. We are focusing our efforts on Realm, the most popular database for mobile devices.
Overview¶
After setting up your Android or iOS project to use the MongoDB Stitch SDKs, you are ready to initialize MongoDB Stitch and connect to the database. The following procedure shows you how to build an app that uses MongoDB Mobile and Atlas to store data.
For an example that uses only MongoDB Mobile to store data in an app, see Build a Local-Only Mobile App.
For a complete example app that uses Sync, see the To-Do tutorial.
Prerequisites¶
If you have not done so yet, refer to the steps in Set up a MongoDB Mobile Project to set up your Android or iOS project to use the MongoDB Stitch SDKs.
Note
The Stitch SDKs require your Atlas cluster to be version 3.4 or higher. To use Sync, your cluster must be version 3.6 or higher.
Connecting Your App to Stitch and Atlas¶
- Android (Java) SDK
- iOS (Swift) SDK
To use the Android SDKs to connect to MongoDB Stitch and sync data between an Atlas cluster to your MongoDB Mobile instance, follow these steps:
First, you establish a remote connection to Atlas and create a RemoteMongoCollection, as follows:
Next, configure Sync by calling the
config()
method on the RemoteMongoCollection.sync() interface:The Config method takes the following properties:
- A ConflictHandler,
which receives both
the remote and local versions of a document when there is a conflict. The
Stitch SDKs provides a built-in
DefaultSyncConflictResolvers
class with two methods, one that uses the local version to resolve the conflict
(
localWins
), and the other that favors the remote version (remoteWins
). - A ChangeEventListener , which is called whenever a change happens locally or remotely. There is no need to handle the change events yourself, but this provides a hook for you to update your app or perform other change-related logic.
- An ErrorListener , which receives errors that occur when syncing local changes (for example, if the operation is not permitted by Stitch rules). Your code can check the error, take the appropriate action, and then call resumeSyncForDocument.
Important
At this point, for all operations on the synced data, be sure to include
sync()
in your calls, like this:[remoteCollection].sync().[operation]
.- A ConflictHandler,
which receives both
the remote and local versions of a document when there is a conflict. The
Stitch SDKs provides a built-in
DefaultSyncConflictResolvers
class with two methods, one that uses the local version to resolve the conflict
(
You can now use the RemoteMongoCollection.sync() interface to perform CRUD operations. For example, you can insert a document into the local database (which will automatically sync with the remote database), and perform custom logic when the insert is complete:
To use the Swift SDKs to connect to MongoDB Stitch and sync data between an Atlas cluster to your MongoDB Mobile instance, you can use the following code:
First, you establish a remote connection to Atlas and create a RemoteMongoCollection, as follows:
Next, configure Sync by calling the
config
method on the RemoteMongoCollection.sync interface.The Configure method takes the following properties:
- A ConflictHandler,
which receives both the remote and local versions of a document when
there is a conflict. The Stitch SDKs provides a built-in
DefaultConflictHandler
class with two methods, one that uses the local version to resolve the conflict
(
localWins
), and the other that favors the remote version (remoteWins
). - A ChangeEventDelegate , which is called whenever a change happens locally or remotely. There is no need to handle the change events yourself, but this provides a hook for you to update your app or perform other change-related logic.
- An ErrorListener , which receives errors that occur when syncing local changes (for example, if the operation is not permitted by Stitch rules). Your code can check the error, take the appropriate action, and then call resumeSync.
For example, your code to configure Sync might look like this:
Important
At this point, for all operations on the synced data, be sure to include
sync
in your calls, like this:[remoteCollection].sync.[operation]
.- A ConflictHandler,
which receives both the remote and local versions of a document when
there is a conflict. The Stitch SDKs provides a built-in
DefaultConflictHandler
class with two methods, one that uses the local version to resolve the conflict
(
You can now use the RemoteMongoCollection’s
sync
interface to perform CRUD operations. For example, you can insert a document into the local database (which will automatically sync with the remote database), and perform custom logic when the insert is complete: