Confused about Bundling Verses Downloading an Existing Realm DB

Hey everyone,

I apologize if this is something that has been covered add nauseam here but I am absolutely confused after reading various tutorials and realm documentation. I should also offer for context that I am new(ish) to swiftUI and less than a week new to Realm and Mongo DB.

I understand how to have the app create data on the fly (I think) but in my case I want to provide the data as either read only or download it from Atlas or Mongo DB one first run and then populate it into a local Realm DB.

I have tried to load a bundled file into memory (incredibly horribly I am sure) and it gave an error that it needed to be updated. I saved out a new DB and updated the code and it solved that but now its throwing another error about it needed a valid location (which confuses me because if it cleared the error and its a valid realm file then how would it not know the location?)

Anyway is there any tutorials floating around in relation to Swift or SwiftUI and bundling or pulling it in on first run? This might sound silly but I feel like I have read so much and tried so many different approaches that now I am very confused as to what to do.

This is a very good question! Either method will work but they have their trade-offs and use cases.

First though, bundling a Realm is covered in the documentation Bundle A Realm and those docs work so you can almost copy and paste the code.

The advantage of bundling a Realm with static data is… it’s static data. It makes ensuring the user has that data is easy, as it’s built-in and it’s fast (local) and re-usable. It requires no connection to the internet and there’s no cost involved. The downside is that it’s well…static data, so refreshing it is not possible without a new release of the app.

Syncing the data would require the data to be pre-populated in MongoDB Atlas data so when the user first connects it syncs and downloads the data. Nice thing there is you can update and change that data on the fly. Downside is there’s a cost involved and every install of the App will be banging on the server to get said data. That could drive up costs significantly based on the number of users and quantity of data.

If it’s truly static data, like State names or a list of wine grapes - those are not likely to change frequently, if ever, so bundling them makes the most sense. Also if you are relying on that data for say, a popup menu or some other static UI element, bundling is the way to go and has no cost.

On the other hand, if the data may change or is in any way dynamic, updating the server data is a quick an easy way to update everyones app and push out those changes - but there is a cost involved.

Hope that helps conceptually.

If you’re stuck on a coding question, please create a question here or on StackOverflow and we’ll take a look. Keep the code short, include you troubleshooting a description of the specific issue.

Jay

Thank you for the link. I does make it a lot more clear about using a bundled realm. This is more of a swift or apple question but when it says App Services ID is it referring to something on Apples end like an App ID or is it referring to something like an App ID within Mongo?

If it’s the first option I have no idea where to find that lol as the few things I can find don’t seem to work. If it is the later isn’t the Realm DB a local thing so why would it need a Mongo App ID?

Replying to my own comment here.

I was able to sort out the issue and I was just overlooking the fine print that says use realm.configure for a local version and was taking the code for a synced file. I did however get the sync to work though I don’t plan on using it. And I say I got it to work in that it opens the realm correctly but then terminated the app due to a permissions error lol.

Either way I am going to try and find a document with the local file code to use that.

Yay!

If you’re not using Sync the everything in the sync’ing section can be ignored. Also, if that’s the case you don’t have to use realm.configure - that will establish the location of the Realm file if you want it somewhere else, OR if you are using multiple Realm files, which in the case of a local-only use case may not be needed at all.

Using a Local Only Realm is document here

And is just a matter of

let realm = try! Realm()

let results = realm.objects(SomeObject.self) //get some objects

or

try! realm.write {
myObject.someProperty = someValue //update an object
}

Man that is even easier… Thank you for all your help. I saw some of your stuff on SO and was hoping you would respond here. I have loads of questions but I will try and solve them my self first :slight_smile:

Next up I need to figure out using the data. I have confirmed it opened via the consle and I can link it to a view where it comes up as it should thing.value all without error but nothing shows. So now I need to figure out if it is my model that is off or what would cause it to not throw errors but now show the data.

I am using this for the code to set the bundled realm in the app…

class RealmManager: ObservableObject {
    
    func openBundledSyncedRealm() {
        let identifier = "UnitsV2"
        let config = Realm.Configuration(inMemoryIdentifier: identifier)
        let realm = try! Realm(configuration: config)
        print("The bundled realm URL is: \(FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!.path)")
        print("Successfully opened the bundled realm")
    }
}

Is that correct?

I am not sure if that’s correct or not as you have two different things going on.

I believe the goal is to bundle a realm with your app - that would entail creating a Realm file locally and then dragging that file into your XCode project - therefore bundling it. See Bundle A Realm

If you want to use an In Memory realm that’s a different use case; if allows you to work with Realm data in a temporary fashion while the app is active; it does not persist the data. See In Memory Realm

Okay.

That makes more sense. I took in memory as it loaded the bundled database into memory to manipulate (like sorting) or display but if that’s not the same then I very well might have things backwards.

I did create the realm file and drag it into the project so I can see it there. It’s in its own folder labeled database and has the .real extension.

I will check that link and see where I might have gotten confused.

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