如何从 iOS (Swift) 连接到 MongoDB [英] How to connect to MongoDB from iOS (Swift)

查看:72
本文介绍了如何从 iOS (Swift) 连接到 MongoDB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 RealmSwift 开发了一个 iOS 应用程序.到目前为止,这工作正常.现在,当我接近发布到 App Store 时,我想设置一些云配置以能够连接到云数据库,但我完全感到困惑.

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.

几周前,我将 Realm Cloud 视为一种选择.现在我只看到 MongoDB Realm - 或者类似的东西.深入研究了一下,发现共有三个组件:Realm DB、Realm Sync 和 Mongo DB Atlas.

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.

如果我的理解是正确的,我必须创建一个 Atlas 集群,我的 Realm 数据库将托管在该集群上,并且我将能够连接和同步到该集群.我说的对吗?

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?

我的主要问题是我不知道如何将它连接到我现有的代码.我不想要用户身份验证或来自 MongoDB 的任何东西,我有自己的,我基本上只有一个数据库,我想同步并连接到它.所以,目前在我通常使用的代码中:

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 {
   ...
}

如何更新它以使用 Atlas Cloud 中的 MongoDB?我浏览了他们的教程",但我仍然很困惑.

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.

我看到一个 Realm(configuration: Realm.Configuration) init 函数,但是如果我应该使用那个函数,我应该如何获得 Realm.Configuration 对象?

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?

非常感谢.

推荐答案

你的困惑是有道理的.文档和教程仍在进行中,并且有点脱节.我认为随着时间的推移它会有所改善.

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 不是完整教程的好地方,但这里有一个非常高级的概述.

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

教程链接 - iOS Swift 教程

完成 Cocoapods 安装

Go through the Cocoapods install

1) 您将在 MongoDB 控制台中创建一个集群

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

2) 在该集群中,您将创建一个 Realm '应用程序'

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

3) 在您要设置的 Realm应用程序"中:

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

  • 同步(开发模式)

  • Sync (development mode)

用户->提供商->电子邮件/密码验证

Users->Providers->Email/Password Authentication

您的应用将有一个 AppId,它可以在左侧的 Atlas 控制台中找到,就在应用名称的右侧(这是一个文档按钮,您可以点击进行复制).

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

然后,在您的 XCode Realm 项目中,您将使用 cocoapods 进行设置以安装 RealmSwift.

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

现在回答您的问题:

如何将其连接到我现有的代码.

how to connect it to my existing code.

添加一个结构体,它是到你的 Atlas Realm 项目的连接字符串

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

导入 RealmSwift

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

这篇关于如何从 iOS (Swift) 连接到 MongoDB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆