Session on what are architecture components and how to use them. Apart from Room persistence library how to use cloud firestore to store and retrieve data which supports offline capability by default.
Also mentioned about the new announcement made by firebase team during the firebase summit 2019 and how to use firebase extension and app distribution.
6. ViewModel ● Designed to store and manage UI-related
data with lifecycle awareness
● Live through the configuration changes
● Caution: ViewModel does not replace
onSaveInstanceState()
8. LiveData ● Observable data holder class
● Respect lifecycle of app components, such
as activities, fragments or services
● Only notifies active observers about updates
● Inactive observers registered to watch
LiveData objects aren't notified about
changes
10. Why
LiveData?
● No memory leaks
● No crashes due to stopped activities
● No more manual lifecycle handling
● Always up to date data
11. LiveData which publicly exposes setValue(T) and postValue(T) method
MutableLiveData<Boolean> userIdLiveData = new MutableLiveData()
userIdLiveData.setValue(true)
MutableLiveData
12. LiveData subclass which may observe other LiveData objects and react on
OnChanged events from them
LiveData liveData1 = ...;
LiveData liveData2 = ...;
MediatorLiveData liveDataMerger = new MediatorLiveData<>();
liveDataMerger.addSource(liveData1,value ->liveDataMerger.setValue(value));
liveDataMerger.addSource(liveData2,value ->liveDataMerger.setValue(value));
MediatorLiveData
13. Applies the given function on the main thread to each value emitted by
source LiveData and returns LiveData, which emits resulting values
LiveData userLiveData = ...
LiveData userName = Transformations.map(userLiveData, user -> {
return user.firstName + " " + user.lastName; // Returns String
}
)
Transformations.map
14. Reacts on changes of trigger LiveData, applies the given function to new
value of trigger LiveData and sets resulting LiveData as a "backing"
LiveData
MutableLiveData userIdLiveData = ...
LiveData userLiveData = Transformations.switchMap(userIdLiveData, id ->
repository.getUserById(id) // Returns LiveData
)
void setUserId(String userId) { this.userIdLiveData.setValue(userId) }
Transformations.switchMap
16. Why
Firebase?
● Available for Android, iOS and Web
● Reduce time for app development
● All in one platform to build app or website
● Helps to grow your business
● Auto scale your app to support billions of users
20. ● It’s NoSQL document database for mobile and web
app development
● Cloud Firestore also offers seamless integration
with other Firebase and Google Cloud Platform
products, including Cloud Functions
● It keeps your data in sync across client apps
through real-time listeners and offers offline support
for mobile and web
Why cloud
firestore?
21. ● Documents and collections structure with powerful
querying capability
● Automatic multi-region data replication with strong
consistency
● Real-time data synchronization
● Android, iOS and Web SDKs with offline data
access
Key
capabilities
22.
23. Collections are like a container which itself do not
store any data but contains document which in
turn stores data
Collection
24. It’s the smallest unit to store data in the database
more set of data type are now supported with
document
Document
26. // Create a new user with a first and last name
val user = HashMap<String, Any>()
user["first"] = "Ada"
user["last"] = "Lovelace"
user["born"] = 1815
// Add a new document with a generated ID
db.collection("users")
.add(user)
.addOnSuccessListener { documentReference ->
Log.d(TAG, "DocumentSnapshot added with ID: ${documentReference.id}")
}
.addOnFailureListener { e ->
Log.w(TAG, "Error adding document", e)
}
27. val city = HashMap<String, Any>()
city["name"] = "Los Angeles"
city["state"] = "CA"
city["country"] = "USA"
db.collection("cities").document("LA")
.set(city) or set(data, SetOptions.merge())
.addOnSuccessListener { Log.d(TAG, "DocumentSnapshot
successfully written!") }
.addOnFailureListener { e -> Log.w(TAG, "Error writing document", e) }
34. class AttendeesViewModel: ViewModel() {
private val _registerAttendee = MutableLiveData<Map<String, Any?>>()
val registerAttendee = _registerAttendee.switchMap {
NetworkRepository.registerAttendee(it)
}
fun setRegAttendeeInfo(map: Map<String, Any?>) {
_registerAttendee.value = map
}
}
35. val attendeesDetails = mapOf(
AppConstant.ATTENDEE_EMAIL to mAttendeeEmail,
AppConstant.TIMESTAMP to FieldValue.serverTimestamp()
)
//Send the attendees details to firestore
mAttendeesViewModel.setRegAttendeeInfo(attendeesDetails)
//Observe the result from firestore
mAttendeesViewModel.registerAttendee.observe(this, Observer {
...
...
})
39. Firebase
App
Distribution
● It makes distributing your apps to trusted testers
easy
● Get stability metrics for all your builds
● Distribute builds using the Firebase console, the
Firebase Command Line Interface (CLI) tool or
Gradle (Android)
40. How to
distribute
app?
● Upload the APK or IPA to App Distribution using the
Firebase console, Gradle, or the CLI tools
● Add the testers you want to try your app. Testers will
receive an email that walks them through the
onboarding process
● When new build is ready for testing, just upload it to
App Distribution. Your testers will be notified that a
new build is available to try out