Realm dataBase transfert on iOS for test

Hello,
I created my first iOS app with a realm database
The database is used offline on the Device
The app runs fine with the xcode simulator

Now I want to test the app on my ipad (plugged on the Mac)
The app is launching but the database is not reachable
The console of Xcode says :
RealDB is located: file:///var/mobile/Containers/Data/Application/FD338F70-94BF-4592-AF9A-A7429DF1/Documents/default.realm
Should I transfer the database manually ? in which folder ?
Thanks a lot for your answers !
Mat

Before going too far into an answer, how did the database ‘get into the simulator’?

e.g. Is this a pre-packaged (bundled) database or is it created as the user enters data?

1 Like

Thanks for your answer
I created the realm database by myself
The user isn’t allowed to modify it.
For each device I want to simulate in Xcode, I need to copy the dataBase in the respective folder.
Xcode give me the way of the folder like this:
RealmDB is located: file:///Users/Platypus/Library/Developer/CoreSimulator/Devices/0850306E-00AC-4EEA-AB14-C1ACA0/data/Containers/Data/Application/3F14D0BF-C230-4C56-8EAF-4DC36E6/Documents/default.realm
But when I want to install the app on my iPad with Xcode simulator, the console give me this way:
RealmDB is located: file:///var/mobile/Containers/Data/Application/FD338F70-94BF-4592-AF9A-A7429DF1/Documents/default.realm
I don’t know where the folder “var” is and I don’t know where I have to copy the dataBase.
I don’t even know if this folder is on the mac or on the iPad! :cry:

Perhaps my question was not clear or detailed enough.

Are you BUNDLING the Realm Database with your app? So for example it’s distributed when the app is?

…or…

Does your code BUILD the Realm database when it’s first run?

…or…

Something else? e.g. how did the Realm file and data get to that location to start with?

The answer will be affected by the above.

ok sorry,
I have populated the data base and it will be bundled and distributed with the app
After this the database won’t be modified by the user

If you’re Bundling your Realm database with the app, then it’s not stored on disk as a separate file.

It’s a read only database that exists within the app bundle. In that case it wouldn’t need to be moved or copied as your code reads it directly from the bundle.

1 Like

ok thank you !
Do you know a tutorial for bundling a realm DataBase in a swiftUI project ?

I do! There are some right on the Swift SDK Tutorial site. There’s a quick start

and then a SwiftUI Guide

1 Like

Hello Jay !
I can’t find an easy way to load the Realm DataBase from the bundle
Could you help me ?
(I’m new in app coding :grimacing:)

Thank you again

ok I just tryed:

import SwiftUI
import RealmSwift

let realmUrl = Bundle.main.url(forResource: “default”, withExtension: “.realm”)
let realm = try ! Realm(fileURL: realmUrl!)

It seems to work…

Great! That looks really close. You may want to remove the . from the realm extension as it’s extraneous.

Here’s how we do it for local only Realms

let config = Realm.Configuration(
    fileURL: Bundle.main.url(forResource: "BundledRealm", withExtension: "realm"),
    readOnly: true) //bundled Realms are read-only

let realm = try! Realm(configuration: config)

let results = realm.objects(DogClass.self).where { $0.name == "Spot" }

Thanks to you, it’s now working on my iPad!
Thank you very much!!

Kind regards

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.