How to Set .Realm File on Realm

How to set .realm file on realm

You can pass a fileURL in the Realm init method

let config = Realm.Configuration(
// Get the URL to the bundled file
fileURL: Bundle.main.url(forResource: "MyBundledData", withExtension: "realm"),
// Open the file in read-only mode as application bundles are not writeable
readOnly: true)

// Open the Realm with the configuration
let realm = try! Realm(configuration: config)

See the documentation for more information. There are more things you can configure here too.

Important note: See the comment in the code above:

Open the file in read-only mode as application bundles are not writeable

To have the realm file sitting in your bundle, you would not be able to write to it, it would be read only. If you want to ship a pre-filled database with your app you can do this in your bundle but if you want it to be writeable you would need to copy it to the documents directory of your app on first launch and then use the version in that directory instead.

resources/files that you add in xcode and include in the 'copy bundle resources' in the build settings are included in your bundle. The bundle is not writeable. To ship a file with your app that you need to be writeable would need to be moved to a suitable folder. usually the app documents folder.

How can I get my Realm Database in a file to open in Realm Studio?

You can find your realm file in the Device File Explorer tab at the bottom right of Android Studio. The location will be /data/data/com.<package-name>/files/default.realm assuming you haven't named your realm file. Simply right-click and "Save As" to a location on your PC and open in Realm Studio. You can't view the realm file on the actual device so you will have to download this realm file every time you want to inspect what's happening in realm. This also means that you need to download the realm file every time you make a change in realm that you want to inspect as you're not actually opening the realm file on the device.

How do I open an iOS simulator local .realm file in Realm Studio

The fact that Realm Studio does not yet open files in response to the open AppleEvent (sent by double-clicking on a Realm file in the finder, or using open on a .realm file) is a known issue.

In the mean time, you can open it via Realm Studio's Open panel by first copying the path, typing / when the Open panel is showing, then pasting the copied path into the text field that has appeared.

How do I create my .realm file/database using the model I've just made in XCode?

You're on the right track! You're correct in that the default.realm file isn't created until Realm is initialized for the first time in your code.

You do that by calling

let realm = try! Realm()

The first time this is called, Realm will collect all of the Realm model classes you've created in your app, and will create a new default.realm file with the appropriate backing schema for those models.

I'm not sure why you'd have .sqlite files in there as well; that's definitely not Realm's doing. It's possible if you have other dependencies in there that rely on data persistence (An analytics library for example), they're internally creating those files.

How do I view my Realm file in the Realm Browser?

Currently the Realm Browser doesn't support accessing databases directly on the device, so you need to copy the database from the emulator/phone to view it. That can be done by using ADB:

adb pull /data/data/<packagename>/files/ .

That command will pull all Realm files created using Realm.getInstance(new RealmConfiguration.Builder().build()) . The default database is called default.realm.

Note that this will only work on a emulator or if the device is rooted.



Related Topics



Leave a reply



Submit