Firebase Database on WatchOS 9

A cloud-hosted, NoSQL database.

Kishan Sadhwani
Simform Engineering
4 min readAug 11, 2023

--

Traditionally, Firebase Realtime Database allowed real-time data synchronization among clients by using its extremely efficient implementation of web sockets.

However, with the release of WatchOS 9, Apple has finally fixed its long-running bug (read more about that in this article), which allowed low-level networking APIs to be used freely, particularly web sockets.

Therefore, the Firebase database can no longer be used in traditional ways.

The Solution: Firebase REST APIs

REST APIs are an interesting alternative way to interact with a real-time database.

We can use all the default methods as we would with any other service, for example:

  • GET
  • POST
  • PUT
  • PATCH
  • DELETE

To use REST APIs in an Apple WatchOS 9 app, you will need to set up a Firebase project and configure your app to use the appropriate APIs.

Base URL & Authentication

To create a REST API call, we need a server URL first, which is typically https://your-project-id.firebaseio.com/. You can find this server URL written on top in Firebase Console once you navigate to the Realtime Database section.

Additionally, all REST API calls must include the query parameter auth to access data protected by Firebase Realtime Database Security Rules.

This parameter is supported by all request types and requires either your Firebase app’s secret or an authentication token.

For more information on authentication for REST APIs, you can head on to Authenticate REST Requests.

Let's have a look at the above-mentioned methods and see how they affect our database.

Consider a sample database with the below data already exists.

1. GET

The GET method can be used to retrieve data from the Firebase Realtime Database.

You can access the data at a specific location by making a GET request to the endpoint.

This will return the data from the specified location in JSON format. In our case, we get all the data residing inside the key Pokemon.

{
"pikachu": {
"body": {
"height": "3 ft",
"weight": "10 kg"
},
"type": "electric"
},
//... and so on.
}

2. POST

The POST method can be used to add new data to the Firebase Realtime Database.

Now, let's add a nice move that our beloved Pikachu loves to use on its enemies. THUNDERBOLT!

This call will add a brand new key moveList to the data.

{
"pikachu": {
"body": {
"height": "3 ft",
"weight": "10 kg"
},
"moveList": {
"-NR3Neeg_EXAWiGdDaop": {
"name": "Thunderbolt",
"pp": "15",
"type": "electric"
}
},
"type": "electric"
},
//... and so on.
}

Note that upon successful completion of the request, the Firebase Realtime Database will generate a unique ID for the new data and return it in the response.

3. PUT

The PUT method can be used to update or replace existing data in the Firebase Realtime Database.

After some time, let’s say our beloved Pikachu has grown enough and we need to update its weight.

This will result in…

{
"pikachu": {
"body": {
"height": "3 ft",
"weight": "12 kg"
},
"type": "electric"
},
//... and so on.
}

Note: When using the PUT method, please be aware that it updates the entire node object, meaning that any keys not included in the parameters will be removed. Therefore, we need to pass the whole object even if only one property is changed.

But hold on, PUT’s not the only option to update data. Using PATCH will resolve the above problem as it will only update the values of the provided keys regardless of how much other data it holds.

4. PATCH

The PATCH method can be used to update existing data in the Firebase Realtime Database.

Using the same request, with the PATCH method this time…

Upon successful completion, the Firebase Realtime Database will update the data at the specified location with the new values.

{
"pikachu": {
"body": {
"height": "3 ft",
"weight": "12 kg"
},
"moveList": {
"-NR3Neeg_EXAWiGdDaop": {
"name": "Thunderbolt",
"pp": "15",
"type": "electric"
}
},
"type": "electric"
},
//... and so on.
}

Voila! The weight was updated and the moveList remains unchanged.

5. DELETE

The DELETE method is used to delete data from the Firebase Realtime Database.

As soon as this call executes, the specified node will be removed from the Firebase Realtime database if it exists.

References

Conclusion

The introduction of WatchOS 9 has brought about changes in how we use Firebase Database. The traditional real-time synchronization method, powered by efficient web sockets, is no longer suitable due to Apple’s bug fix. However, a solution presents itself through Firebase REST APIs. These APIs, encompassing methods like GET, POST, PUT, PATCH, and DELETE, enable interaction with the database. To move forward, set up a Firebase project, configure your app, and explore these methods to interact with your real-time database effectively!

Follow me for the next part, where we will learn about various ways for filtering the data and, of course, observing the Server Sent Events by Firebase.

For more updates on the latest tools and technologies, follow the Simform Engineering blog.

Follow Us: Twitter | LinkedIn

--

--