Accessing Realm Data on iOS Using Realm Studio

Andrew Morgan
Realm Blog
Published in
7 min readMar 5, 2021

The Realm Mobile Database makes it much faster to develop mobile applications. MongoDB Realm Studio is a desktop app that lets you view, manipulate, and import data held within your mobile app’s Realm database.

This article steps through how to track down the locations of your iOS Realm database files, open them in Realm Studio, view the data, and make changes. If you’re developing an Android app, then finding the Realm database files will be a little different (we’ll follow up with an Android version later), but if you can figure out how to locate the Realm file, then the instructions on using Realm Studio should work.

Prerequisites

If you want to build and run the app for yourself, this is what you’ll need:

  • A Mac.
  • Xcode — any reasonably recent version will work.
  • Realm Studio 10.1.0+ — earlier versions had an issue when working with Realms using MongoDB Realm Sync.

I’ll be using the data and schema from my existing RChat iOS app. You can use any Realm-based app, but if you want to understand more about RChat, check out Building a Mobile Chat App Using Realm — Data Architecture and Building a Mobile Chat App Using Realm — Integrating Realm into Your App.

Walkthrough

This walkthrough shows you how to:

  • Install & Run MongoDB Realm Studio
  • Track Down Realm Data Files — Xcode Simulator
  • Track Down Realm Data Files — Real iOS Devices
  • View Realm Data
  • Add, Modify, and Delete Data

Install & Run MongoDB Realm Studio

I’m using Realm Studio 10.1.1 to create this article. If you have 10.1.0 installed, then that should work too.

If you don’t already have Realm Studio 10.1.0+ installed, then download it here and install.

That’s it.

But, when you open the app, you’re greeted by this:

There’s a single button letting me “Open Realm file,” and when I click on it, I get a file explorer where I can browse my laptop’s file system.

Where am I supposed to find my Realm file? I cover that in the next section.

Track Down Realm Data Files — Xcode Simulator

If you’re running your app in one of Xcode’s simulators, then the Realm files are stored in your Mac’s file system. They’re typically somewhere along the lines of ~/Library/Developer/CoreSimulator/Devices/???????/data/Containers/Data/Application/???????/Documents/mongodb-realm/???????/????????/???????.realm.

The scientific way to find the file’s location is to add some extra code to your app or to use a breakpoint.

While my app’s in development, I’ll normally print the location of a Realm’s file whenever I open it. Don’t worry if you’re not explicitly opening your Realm(s) in your code (e.g., if you’re using the default realm) as I’ll cover the file search approach soon. This is the code to add to your app once you’ve opened your realm — realm:

If you don’t want to edit the code, then an Xcode breakpoint delivers the same result:

Once you have the file location, open it in Realm Studio from the terminal:

Less scientific but simpler is to take advantage of the fact that the data files will always be of type realm and located somewhere under ~/Library/Developer/CoreSimulator/Devices. Open Finder in that folder: open ~/Library/Developer/CoreSimulator/Devices and then create a "saved search" so that you can always find all of your realm files. You'll most often be looking for the most recent one.

The nice thing about this approach is that you only need to create the search once. Then click on “Realms,” find the file you need, and then double-click it to open it in Realm Studio.

Track Down Realm Data Files — Real iOS Devices

Unfortunately, you can’t use Realm Studio to interact with live Realm files on a real iOS device.

What we can do is download a copy of your app’s Realm files from your iOS device to your laptop. You need to connect your iOS device to your Mac, agree to trust the computer, etc.

Once connected, you can use Xcode to download a copy of the “Container” for your app. Open the Xcode device manager — ”Window/Devices and Simulators.” Find your device and app, then download the container:

Note that you can only access the containers for apps that you’ve built and installed through Xcode, not ones you’ve installed through the App Store.

Right-click the downloaded file and “Show Package Contents.” You’ll find your Realm files under AppData/Documents/mongodb-realm/<app-name>/?????. Find the file for the realm you're interested in and double-click it to open it in Realm Studio.

View Realm Data

After opening a Realm file, Realm Studio will show a window with all of the top-level Realm Object classes in use by your app. In this example, the realm I’ve opened only contains instances of the Chatster class. There's a row for each Chatster Object that I'd created through the app:

If there are a lot of objects, then you can filter them using a simple query syntax:

If the Realm Object class contains a List or an EmbeddedObject, then they will show as blue links-in this example, conversations and userPreferences are a list of Conversation objects and an embedded UserPreferences object respectively:

Clicking on one of the UserPreferences links brings up the contents of the embedded object:

Add, Modify, and Delete Data

The ability to view your Realm data is invaluable for understanding what’s going on inside your app.

Realm Studio takes it a step further by letting you add, modify, and delete data. This ability helps to debug and test your app.

As a first example, I click on “Create ChatMessage” to add a new message to a conversation:

Fill out the form and click “Create” to add the new ChatMessage object:

We can then observe the effect of that change in our app:

I could have tested that change using the app, but there are different things that I can try using Realm Studio.

I haven’t yet included the ability to delete or edit existing messages, but I can now at least test that this view can cope when the data changes:

Summary

In this article, we’ve seen how to find and open your iOS Realm data files in Realm Studio. We’ve viewed the data and then made changes and observed the iOS app reacting to those changes.

Realm Studio has several other useful features that I haven’t covered here. As it’s a GUI, it’s fairly easy to figure out how to use them, and the docs are available if you get stuck. These functions include:

  • Import data into Realm from a CSV file.
  • Export your Realm data as a JSON file.
  • Edit the schema.
  • Open the Realm file from an app and export the schema in a different language. We used this for the WildAid O-FISH project. I created the schema in the iOS app, and another developer exported a Kotlin version of the schema from Realm Studio to use in the Android app.

References

If you have questions, please head to our developer community website where the MongoDB engineers and the MongoDB community will help you build your next big idea with MongoDB.

Originally published at https://developer.mongodb.com.

--

--

Andrew Morgan
Realm Blog

Andrew is a Staff Developer Advocate at MongoDB. He’s a fan of SwiftUI and the Realm mobile database