Interacting with Firebase in SwiftUI with Combine

Tram Ho

So true to the title, today I will show you how we can read and process data in firebase with a combine guy. Then let’s get started.

Configure FireBase for App

Then Firebase is a real-time database service provided by Google and operating on the cloud platform. It helps developers to quickly develop mobile applications by simplifying database operations. And in today’s post, I will use the realtime database guy, it is: the guy that stores and synchronizes real-time user data. Applications that support this feature can store and retrieve data from the server very quickly. The data is stored in a database system that supports NoSQL and is located on the cloud server platform. Data is written and read with the lowest time in milliseconds.

And to configure, you must first go to /settings/general/ of your firebase. To download GoogleService-Info.plist and then put it in our app. Then it’s a bit different from the UIKit side, on the SwiftUI side we will call the configure function in the init of the total view like this.

Screen Shot 2021-07-21 at 09.02.12.png

So we’re done with the firebase configuration setup. Next, we will start creating a simple application to demo today’s lesson.

Create a service for the firebase guy

Get all the data of a reference

Then, at a glance, we can see that I’m using Future, this is a special puller that will emit a single value, then terminate or fail. It will execute a Promise. It’s a closure with type Result, so there will be 1 of 2 cases:

Success : emitting Output

Failure : emitting Error

So when it only returns data after all the data has been retrieved. As for decoding data in firebase, I will use https://github.com/alickbass/CodableFirebase it will help you decode and encode quickly and easily.

Handling the data of a reference

That’s as I said above, when the jobs are done, it will send us a data to report that the data interaction has been successful. Since here is the post method, we do not need it to return any data, so here Future<Void, E>.

MVVM with Combine

ViewModel side

Here because it is an MVVM model, it will have two guys, input and output

On it I am using AnyPublisher, it is Type-erased publisher With AnyPublisher, it is not possible to call the send() function. This class has wrapped and hidden many methods & properties of Publisher. And to convert the user input into the desired output, we will use the following:

flatMap, it will transform one publisher into another puller. But on the other side of rxSwift, when transformed, the data type it returns becomes a very lumpy lump. So then we call a function eraseToAnyPublisher(). Then it will turn our existing data into the value type for the new object AnyPublisher. And that data will be returned to the View for processing.

View side

It will then subscribe to the stream’s data returned data notifications. Then when the output side has return data, we will use it to capture that returned data, then when it’s done, it just needs to be assigned to the list. Because our list variable is State, when its data changes, it will automatically update the views that are using that state variable.

Then come here, you have got yourself all the tools to network with all kinds of api, not just the firebase guy anymore.

Screen Shot 2021-07-21 at 09.43.25.png

Here is the project link for your reference : https://github.com/thanduchuy/SwiftUI-FirebaseAppList

Share the news now