One Firebase Realtime
Firebase Realtime Database is a cloud-hosted NoSQL database that allows you to store and sync data between your users in real-time.
Checkout the full features here
Example Usage:
This is content of RequestModel and ResponseModel:
The RealtimeRequest
data class is a Kotlin model used for sending data to a Firebase Realtime Database. It has two nullable properties: name
(a string) and age
(an integer), both initialized to null
by default. The class includes a toMap
function, marked with the @Exclude
annotation (likely to prevent it from being serialized), which converts the object's properties into a HashMap
with keys "name" and "age" mapped to their respective values. This toMap
function facilitates the transformation of the data class into a format suitable for database operations in Firebase.
The RealtimeResponse
data class is a Kotlin model designed to represent a response from a Firebase Realtime Database. It includes three nullable properties: id
(a string for the unique identifier), name
(a string for the name), and age
(an integer for the age). Each property is initialized to null
by default, making them optional. This class is typically used to map the data retrieved from the Realtime Database into a Kotlin object, allowing for easy access and manipulation of the database response data within an application.
And here content is RealtimeRepository
Explanation:
Class and Interfaces
This line declares the RealtimeDataSourceOne
class which implements the RealtimeRepository
interface and extends OneFirebaseRealtime
. This means it must provide concrete implementations for the methods defined in RealtimeRepository
.
Properties
This property provides a reference to the "users" node in the Firebase Realtime Database. It is overridden from the parent class or interface.
Functions
realtimeAdd
This function adds a new entry to the database.
Parameters: Takes a
RealtimeRequest
object.Return Type: Returns a
Flow<Resource<Boolean>>
.Functionality:
Uses
callbackFlow
to emit database operation results asynchronously.Calls
setValue
to add the data to the database, converting it to a map usingdata.toMap()
.Uses
trySend
to send the result of the operation.awaitClose
ensures the flow is properly closed when no longer needed.
realtimeRemove
This function removes an entry from the database.
Parameters: Takes a
String
ID.Return Type: Returns a
Flow<Resource<Boolean>>
.Functionality:
Uses
callbackFlow
to handle asynchronous removal.Calls
removeValue
to delete the entry with the given ID.Sends the result using
trySend
.Ensures proper closure with
awaitClose
.
realtimeList
This function retrieves a list of entries from the database.
Parameters: None.
Return Type: Returns a
Flow<Resource<List<RealtimeResponse>>>
.Functionality:
Uses
callbackFlow
to emit the list of entries asynchronously.Calls
getListValue
to fetch the data and converts it to a list ofRealtimeResponse
objects.Uses
trySend
to send the result.Closes the flow with
awaitClose
.
realtimeEdit
This function edits an existing entry in the database.
Parameters: Takes a
String
ID.Return Type: Returns a
Flow<Resource<Boolean>>
.Functionality:
Uses
callbackFlow
for asynchronous editing.Creates a
hashMapOf
with the field to be updated (name
to "wahid").Calls
updateValue
to update the entry.Sends the result using
trySend
.Closes the flow with
awaitClose
.
getData
This function retrieves a specific entry from the database.
Parameters: Takes a
String
ID.Return Type: Returns a
Flow<Resource<RealtimeResponse>>
.Functionality:
Uses
callbackFlow
for asynchronous data retrieval.Calls
getValue
to fetch the entry with the given ID and converts it to aRealtimeResponse
object.Uses
trySend
to send the result.Ensures proper closure with
awaitClose
.
Last updated