Accessing Realm Data on iOS Using Realm Studio
Rate this tutorial
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.
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.
This walkthrough shows you how to:
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.
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
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 –
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
realmand located somewhere under
~/Library/Developer/CoreSimulator/Devices. Open Finder in that folder:
open ~/Library/Developer/CoreSimulator/Devicesand 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.
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.
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
Chatsterclass. There's a row for each
ChatsterObject that I'd created through the app:
If the Realm Object class contains a
EmbeddedObject, then they will show as blue links—in this example,
userPreferencesare a list of
Conversationobjects and an embedded
Clicking on one of the
UserPreferenceslinks brings up the contents of the embedded object:
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
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:
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.
- Import data into Realm from a CSV file.
- Export your Realm data as a JSON file.
- Edit the schema.