The Question Mark - blog by Mark Volkmann

Realm

Overview

Realm is an object database that can be used by web applications and mobile applications (Android and iOS). The data can remain local on each device or it can be synced with the cloud. It provides an alternative to SQLite.

Realm is a popular alternative to using Core Data and CloudKit which are specific to Apple platforms.

Changes in a local database are synced with a MongoDB Atlas database in the cloud. This enables all users of an app to have a consistent view of the data. Realm has built-in conflict resolution to handle concurrent updates.

Partitioning can be used to limit the data that is synced in each app. This has the following benefits:

  • avoid copying sensitive data to devices the should not have access
  • reduce the amount of data copied to each device
  • reduce changes related to bandwidth usage.

Realm was purchased by MongoDB in 2019 for 39 million dollars.

Realm supports multiple authentication approaches including username/password, JWT, Google, and Apple authentication.

Realm supports serverless functions implemented in JavaScript that can use NPM modules.

Realm supports database triggers for:

  • authentication: user registration, login, and user delete
  • data: insert, update, replace, and delete operations
  • scheduled operations similar to CRON

Realm Studio is a visual tool for designing Realm databases and adding, viewing, and editing data. It is similar to Compass for MongoDB databases.

Realm Studio provides two-way live updates. Changes made in the Realm Studio appear in running apps and changes made in running apps appear in Realm Studio.

Setup

  1. Browse MongoDB Cloud Services.
  2. Click “Try Free”.
  3. Enter the required information to create an Atlas account.
  4. Click the “Build a Database” button.
  5. Select the “M0” button which is free and is “for learning and exploring MongoDB in a cloud environment”.
  6. Click the “Create” button.
  7. Enter a username and password to associate with the new database.
  8. Click the “Create User” button.
  9. Click the “Finish and Close” button.
  10. In the dialog that appears click the “Go to Databases” button.
  11. Click the “App Services” tab.
  12. Select “Real-time Sync”.
  13. Click the “Next” button.
  14. Under “Name your Application” enter a name and click the “Save” button. I entered “RealmDemo”.
  15. Click the “Create App Service” button.
  16. Select “Swift (iOS + SwiftUI)”.
  17. Click the “Download front-end code” button.
  18. Click the “Close” button.
  19. In the Finder, navigate to the “Downloads” directory.
  20. Double-click the zip file that was downloaded whose name begins with the application name entered above to unzip it.
  21. In the directory created by unzipping, double-click the .xcodeproj file to open it in Xcode.
  22. Return to the web browser.
  23. In the left nav, click “Authentication”.
  24. Verify that “Email/Password” authentication is enabled (“On” by default).
  25. Click the “Edit” button on the row for “Email/Password”.
  26. Choose a “User Confirmation Method”.
  27. Choose a “Password Reset Method”.
  28. In the left nav, click “Device Sync” to enable syncing. No sync configuration changes are needed.

Creating an iOS App

  1. Launch Xcode.
  2. Create a new project for an iOS app.
  3. Select File … Add Packages…
  4. In the search input paste “https://github.com/realm/realm-swift.git”. This will find a package named “realm-swift”.
  5. Add this package to obtain both “Realm” and “RealmDatabase”.
  6. Create model structs. 23:00 in video

TODO: Add more detail to this page!