HomeLearnHow-to

Accessing Realm Data on iOS Using Realm Studio

Published: Feb 08, 2021

  • Realm
  • Realm Studio
  • Mobile
  • ...

By Andrew Morgan

Share

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

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:

Realm Studio opening view, with a button to "Open Realm file"

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:

1print("User Realm User file location: \(realm.configuration.fileURL!.path)")

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

Using an Xcode breakpoint to find the location of your Realm data file

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

1open /Users/andrew.morgan/Library/Developer/CoreSimulator/Devices/E7526AFE-E886-490A-8085-349C8E8EDC5B/data/Containers/Data/Application/C3ADE2F2-ABF0-4BD0-9F47-F21894E850DB/Documents/mongodb-realm/rchat-saxgm/60099aefb33c57e9a9828d23/%22user%3D60099aefb33c57e9a9828d23%22.realm

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.

Creating a saved search to always be able to find your apps' realms

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:

Download your app's container from your iOS device using Xcode

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:

View all top-level Realm Objects in this realm file

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

Search for any Chatster object where displayName matches Janey

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:

Displaying a User object that contains a list of Conversation objects and an embedded UserPreferences object

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

Displaying a User object that contains a list of Conversation objects and an embedded UserPreferences 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:

Clicking on "Create ChatMessage" button

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

Fill in form to create new ChatMessage object

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

Observe new message appearing in the iOS 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:

Observe new messages disappearing and changing in the iOS app

#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.

MongoDB Icon
  • Developer Hub
  • Documentation
  • University
  • Community Forums

© MongoDB, Inc.