LoginSignup
14
7

More than 1 year has passed since last update.

2020-11-25時点でMongoDB RealmをiOSで使う場合の注意点

Last updated at Posted at 2020-11-25

経緯

いま、iOS/Android(おそらくWebも)対応予定のアプリをprivateで書いています。
クロスプラットフォームでデータを同期するようなアプリとなる予定なので、記事タイトルにあるMongoDB Realmを利用することにしました。

というような経緯で、MongoDB RealmをiOSアプリで導入しようとしたら、2020-11-25時点でいくつか注意する点があったので、メモがてら残しておきます。

Be sure to select MongoDB version 4.4

見出しの通りなのですが、MongoDB Realmでは、MongoDB version 4.4(2020-11-25時点)である必要があります。

MongoDBが2019年にRealmを買収したため、2020年頃にRealmとMongoDBを統合したサービスに刷新され、以下の構成でworkするようになっています。
スクリーンショット 2020-11-25 20.39.07.png
https://www.mongodb.com/realm より

稲妻アイコンがRealmで葉っぱアイコンがMongoDBです。

MongoDB Realm利用者は、AWS / Google Cloud / Azure上にMongoDB Atlasクラスタを構築する必要があります。
このあたりは、MongoDB RealmがWeb UI(Realm UIと呼ばれています)を提供してくれているので、それ経由で簡単に構築できます。なのでRealm UIに従いサクサク進めていくと、見落としがちなのですが(私がそうでした :cry:)、MongoDBのバージョンは4.4である必要があります。
default versionが4.2になっていたり、いくつかのregionは4.2までしか利用できない、という状況なので、利用する際には気をつけてください :pray:

Choose your preferred provider and region, tier, and additional settings. As you build your cluster, Atlas displays the associated costs at the bottom of the page.

Be sure to select MongoDB version 4.4 for your cluster if you wish to use sync!

https://docs.mongodb.com/realm/get-started/create-realm-app/#b-create-an-atlas-cluster

(ドキュメントにはちゃんと書いてあります。sync機能を使う場合はと記載されているのですが、MongoDB Realmを採用するにも関わらず、sync機能を使わないケースはあまりない気がするので、実質必須と言ってしまって良い気がします。)

SwiftPM経由でRealmを導入できない?

こちらのドキュメントを読みつつiOSアプリにRealmの導入を進めていると、
こちらに記載されている通り、RealmはSwiftPM対応されているとのことなので、SwiftPM経由でRealmを依存に追加しました。

追加後もドキュメントに従い、import文を追加、

import RealmSwift

さらに下記を追加したのですが、Appは見つからない、というようなエラーが出てbuildできませんでした :cry:

let app = App(id: YOUR_REALM_APP_ID) // Replace YOUR_REALM_APP_ID with your Realm app ID

「import文が足りない?SwiftPMにおける依存の追加に誤りがある?」:thinking: などと自身の見落としを疑いドキュメントを改めて読んでみたのですが、特に問題はなさそうでした。
そもそもApp.swiftが存在していないのでは、ということで、realm-cocoaのPackage.swiftを見てみると、App.swiftexcludeされており、こちらが原因のように見えました :cry:
試しにCocoaPods経由で追加すると無事buildが通るようになりました。

ドキュメント上では、SwiftPM経由で追加した場合にはApp.swiftは無い(利用できない)、というようなことは何も記載されていないにも関わらずexcludeされている原因が気になったのでPRを追ってみました。

Package.swiftのexclude対象としてApp.swiftが追加されたのは、こちらのPRですが、このPRではRealmApp -> Appというrenameをしただけのようで、rename前のRealmApp.swiftがexclude対象になったのは、こちらのPRのようでした。
PRコメントとしてはDescription TK.と書かれているのみだったので、
CHANGELOG.mdを読んでみると、このPRでMongoDB統合をサポート開始したようでした。

Add support for next generation sync. Support for syncing to MongoDB instead of Realm Object Server. Applications must be created at realm.mongodb.com

ただ、知りたかったRealmApp.swiftPackage.swiftにてexcludeされるようになった理由については特に記載はなさそうでした。commit logもこれだけで、理由はクリアにならなかったので、issueで聞いてみるとことにしました。

issueを立てようとすると、下記のISSUE_TEMPLATEが存在し、

!!! MANDATORY TO FILL OUT !!!
<!---

**Questions**: If you have questions about HOW TO USE Realm, ask on
[StackOverflow](http://stackoverflow.com/questions/ask?tags=realm),
or in our [Swift Forum](https://forums.realm.io/c/swift) or [Obj-C Forum](https://forums.realm.io/c/objc).

**Feature Request**: Just fill in the first two sections below.

**Bugs**: To help you as fast as possible with an issue please describe your issue
and the steps you have taken to reproduce it in as many details as possible.

-->

## Goals
<!--- What do you want to achieve? -->

## Expected Results
<!--- What did you expect to happen? -->

## Actual Results
<!--- What happened instead?
e.g. the stack trace of a crash
-->

## Steps for others to Reproduce
<!--- What are steps OTHERS can follow to reproduce this issue? -->

## Code Sample
<!---
Provide a code sample or test case that highlights the issue.
If relevant, include your model definitions.
For larger code samples, links to external gists/repositories are preferred.
Alternatively share confidentially via mail to help@realm.io.
Full Xcode projects that we can compile ourselves are ideal!
-->

## Version of Realm and Tooling
<!---
[In the CONTRIBUTING guidelines](https://git.io/vgxJO), you will find a script,
which will help determining some of these versions.
-->
Realm framework version: ?

Realm Object Server version: ?

Xcode version: ?

iOS/OSX version: ?

Dependency manager + version: ?

今回の件だと、テンプレート中に記載があるSwift Forumのほうが適していそう、かつ、ぱっと見、同じような質問はなさそうだったので、そちらで質問してみることにしました。

で、いまWhy is App.swift exclude from Package.swift?というタイトルの投稿をしてみたのですが、どうやら承認が必要なようで、まだ表示されていません。

We've received your new post but it needs to be approved by a moderator before it will appear. Please be patient.

You have 1 post pending.

なにか返信など来たら追記したいと思います。

2020-11-25 22:43追記

当記事を公開していたら、早速返信してもらうことができました。(めちゃくちゃ早い)
https://developer.mongodb.com/community/forums/t/why-is-app-swift-exclude-from-package-swift/12160

スクリーンショット 2020-11-25 22.44.26.png
スクリーンショット 2020-11-25 22.45.30.png

ということで、Realmのsync機能はSPMをサポートしていないようです :pray:
素直にCocoapods or Carthage経由で利用しましょう。
(ドキュメントに書いておいてくれても良さそうな気もしますが、sync機能はβだったりするので、整備中なのかもしれません)

さいごに

なんというか、まとまりのない文章になってしまいましたが、言いたかったのは下記2点です。

  • Be sure to select MongoDB version 4.4
  • SwiftPM経由でRealmを導入できない?

以上です!

14
7
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
7