ITNEXT

ITNEXT is a platform for IT developers & software engineers to share knowledge, connect, collaborate, learn and experience next-gen technologies.

Follow publication

RxSwift x Firestore

--

Cloud Firestore, one of Firebase's products, is a cloud-hosted, noSQL database with live synchronization and offline support for mobile and web so you can build responsive apps that work regardless of network latency or Internet connectivity.

I don’t want to go into too much about the Firebase ecosystem because it would take a year to break down how incredible it is. The main purpose of the article is to show you how to use Cloud Firestore with RxSwift professionally.

First of all, you have to set up some stuff.

Second of all, let’s drink water and relax a little bit.

Coding time!

1. Create FirestoreClient Management.

Here, we are going to create a FirestoreClient class that applies the Singleton Pattern.

  • persistenceEnabled: By default, offline persistence is enabled, so if you don’t want to cache a copy of the Cloud Firestore data that your app is actively using, then set it to false. For a detailed overview, see this document.

2. Write data to Cloud Firestore.

For example, we are going to write a function to send a message.

  • Firstly, write a message request and response model that you want to send and receive data from Cloud Firestore.
  • Next, write a send message function.
  • Let’s take a look at line 7, addDocument function requires an NSDictionary for its parameter, so let’s create an extension that converts Encodable object into an NSDictionary. Encodable+Extension.swift

If all go well, we’ll have a structure data like this:

For a detailed overview, see this document

3. Read data from Cloud Firestore.

You already knew how to send a message; nevertheless, to make this example more clear, we are going to write another function to retrieve all of the messages from the messages collection.

Let’s create some Codable wrappers to parse the data once we’ve received a snapshot from the Cloud Firestoreline 9.

  • Decode a single document
  • Decode multiple documents

These wrappers took inspiration from this article: [Swift]Firestoreのdocument取得を見やすく書けるようにしてみる

Finally, the listenChatMessage() function will return an Observable that contains an element value which is your current database from Cloud Firestore. Then, whenever the database changes as a result of an operation such as adding, deleting, or updating… This function will return an Observable with an element value of your modified database right away.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Published in ITNEXT

ITNEXT is a platform for IT developers & software engineers to share knowledge, connect, collaborate, learn and experience next-gen technologies.

No responses yet

Write a response