How to connect to MongoDB from iOS (Swift)

心不动则不痛 提交于 2021-01-28 14:10:23

问题


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.


回答1:


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!))



回答2:


I've never used Realm as anything other than an embedded database inside the iOS app. If you need to store something on the server side you should implement an API that does the writing to your MongoDB (or whatever database you use). Connecting the iOS app directly to a server side database seems like an anti-pattern to me.




回答3:


Realm Sync connects to the Atlas deployment as needed to synchronize the data. My understanding is you do not need to connect to Atlas directly in your code.

Documentation provides some information and a few pages down there is sample code.



来源:https://stackoverflow.com/questions/62375536/how-to-connect-to-mongodb-from-ios-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!