Search code examples
iosswiftmongodbrealm

How to connect to MongoDB from iOS (Swift)


I have developed an iOS application using RealmSwift. This works fine so far. Now, as I'm getting closer to publishing into App Store, I wanted to set up some cloud configuration to be able to connect to a cloud database, but I just got totally confused.

A couple of weeks ago I saw Realm Cloud as an option. Now I only see MongoDB Realm - or something like that. I digged into a bit, and found that there are three components: Realm DB, Realm Sync and Mongo DB Atlas.

If my understanding is correct, I have to create an Atlas Cluster, on which my Realm Database will be hosted and to which I will be able to connect and sync. Am I correct?

My main problem is that I have no idea how to connect it to my existing code. I don't want user authentication or anything from MongoDB, I have my own, I basically have a DB only, which I want to sync and connect to. So, currently in the code I usually use:

let realm = try! Realm()
try! realm.write {
   ...
}

How can I update it to use the MongoDB in the Atlas Cloud? I went through their 'tutorials' but I'm still way too confused.

I see a Realm(configuration: Realm.Configuration) init function, but if I should use that one, how should I get a Realm.Configuration object?

Also, what does partition key means?

Thanks a lot.


Solution

  • Your confusion is justified. The docs and tutorials are still a work in progress and a bit disjointed. I think over time it will improve.

    SO is not a good place for a full tutorial but here's a very high level overview.

    A link to the tutorial - iOS Swift Tutorial

    Go through the Cocoapods install

    1) Your going to create a Cluster in the MongoDB console

    2) Within that cluster you're create a Realm 'app'

    3) Within that Realm 'app' you're going to set up:

    • Sync (development mode)

    • Users->Providers->Email/Password Authentication

    Your app will have an AppId, which can be found in the Atlas console on the left, right next to the app name (it's a document button you can click on to copy).

    Then, in you XCode Realm project, you'll set it up using cocoapods to install RealmSwift.

    Now to your question:

    how to connect it to my existing code.

    Add a struct, which is the connection string to you Atlas Realm project

    import RealmSwift

    struct Constants {
        // Set this to your Realm App ID found in the Realm UI.
        static let REALM_APP_ID = "your app id"
    }
    

    then, when you want to authentication, you'll do this

    let app = RealmApp(id: Constants.REALM_APP_ID)
    app.login(withCredential: AppCredentials(username: username, password: password)) { user, error in
    

    once you've authenticated, to access realm use this

    guard let user = app.currentUser() else {
       fatalError("Must be logged in to access this view")
    }
    
    let realm = try! Realm(configuration: user.configuration(partitionValue: user.identity!))