How is that Push-based synchronization implemented in NIA? provide a better guide for beginners #886
-
As per comments provided by devs it really help us to understand the code implementations but when it comes to Push-based sync implementation it is hard to understand how it is working and how can i implement this in my project without knowing network data source side implementation. Because all over the internet there is only one article which talk about Push-based sync implementation in android by @tunjid but there is also not clearly mentioned how they implemented it in NIA how's that |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Now In Android has 2 data types: Data types in Now in AndroidNews Resource
Topic
Both of these are persisted in the backend in a database that allows for a simple RESTful API to be built on top of it. Since Now In Android is a read only app at the moment, we have 2 REST endpoints:
Modern database solutions including FireStore, PostgreSQL, MySQL, MongoDB and so on provide write triggers. That is, anytime a record is written, a callback can be invoked to perform some action. This is the abstraction the offline first approach in NiA is built on. This abstraction is called change list invalidation and is a publish-subscribe pattern implementation. Change list invalidationChange list invalidation is an incremental cache invalidation strategy most similar to the git version control system. It allows apps to be kept up to date in near real time. It doesn't focus on how often an app hit the server's source of truth (it should hit it as often as it needs to), but how efficiently the server responds to requests. It has the following premise:
The pros and cons of this approach are as follows: Pros:
Cons:
Firebase FireStore as the change list server for Now In AndroidThe following processes are used to implement a change list server with Firebase Cloud Functions:
Write triggersFirebase FireStore has triggers that may be called upon any write to the database. These writes are only triggered if the underlying dataset is actually changed. That is, if the same data was written, the trigger will not be pulled. Change List TransactionsWhen the change list cloud function is triggered, a transaction is run where:
Client Notification and SyncUpon completion of the transaction above, Firebase Cloud Messaging can be used to ping the client and instruct it to sync with the server. The client then:
This process ensures that even if notifications are missed, the client can sync at its earliest opportune time, and still be up to date. Changelist invalidation as a push based offline first approachThe above works well for Now In Android because users are essentially subscribed to all news resources so all updates to news can be pushed to the app. If an app is not interested in all updates that occur server-side the app should only subscribe to the set of topics its interested in, and should probably use a pull based approach for others. Miscellaneous notesThe change list server could have been built on any kind of database. In fact if your app uses FireStore you should just use the native FireStore SDK. Now In Android uses regular RESTful APIs as that is what most apps consume. |
Beta Was this translation helpful? Give feedback.
Now In Android has 2 data types:
Data types in Now in Android
News Resource
Topic