# Accessing Realm Data on iOS Using Realm Studio

The Realm makes it much
faster to develop mobile applications. 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 Atlas Device
    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 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 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`:

``` swift
print("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:

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

``` bash
open /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.

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//?????`. 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

-   GitHub Repo for RChat App.
-   Read Building a Mobile Chat App Using Realm – Data Architecture to understand the data model and partitioning strategy behind the RChat app.
-   Read Building a Mobile Chat App Using Realm – Integrating Realm into Your App to learn how to create the RChat app.
-   If you're building your first SwiftUI/Realm app, then check out Build Your First iOS Mobile App Using Realm, SwiftUI, & Combine.
-   GitHub Repo for Realm-Cocoa SDK.
-   Realm Cocoa SDK documentation.
-   MongoDB's Realm documentation.

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