RxJava2 get latest value if it fresh enought or request new one - rx-java2

I need observable which will be able to return latest emitted data or request new one if it is expired while at the same time allowing force updates.

Related

Flutter Best Practices to Continuously Poll for New Data while on a Page which renders that Data?

I have an App written in Flutter, and I wish to have the App immediately render new Objects found in the Database, but the best way I can think of achieving this is firing a GET request to the Backend Database and call "setState()" every 5~10 seconds. This is so that when a user is in his "Order" table in-phone, and places a new "Order" from his/her laptop, I want that "Object" to show up in that users table in phone, without any page navigations or reloading the page.
The Table is implemented as a ListView.Builder()
If you manage also the backend I think one way could be to use websocket. When an order is inserted you call an API that emits an event to a websocket, your Flutter app that uses websocket can listen for this event and eventually rebuild the data.
In this way you don't need to call the GET api every N seconds but the server "calls you".
Here some flutter documentation about websockets: https://flutter.dev/docs/cookbook/networking/web-sockets

How to notify a user when JSON of a webpage changes in Flutter?

I wanted to know if there's a way I could get a JSON from a website online, and get to know when the JSON is updated, for example let's say its updated once in every 48 hours, so when the JSON gets updated I can notify the user of a change in the webpage.
An app version of something like https://visualping.io/ does basically.
You can achieve such a task by following these steps:
Fetch data from a RestAPI using http get method.
Store the data locally
Run the same method (which sends the http request to fetch data) at regular intervals of time (48 hours, in your case). How do I run a reoccurring function, in Dart?
Make use of state management approaches such as the provider package to listen to the changes in the locally stored data
Update the user whenever the provider offers a changed value
I hope it was of any help.

How to get a response from Moodle when a student start a course or completes a course?

Each time student starts a course or completes a course, i need to update my system indicating that the course is started or completed depending on the response from moodle. How can I achieve this in moodle
You can use event observers in moodle. Observe the events you mentioned and perform the action required.
https://docs.moodle.org/dev/Event_2#Event_observers

REST APIs: How to ensure atomicity?

I am developing a small REST API. As I got into analyzing all the possible failure scenarios, which I have to handle to create a reliable and stable system, I went into thinking about how to make my APIs atomic.
If we take a simple case of creating a contact through the POST API.
The server gets the POST request for the new contact.
Creates the contact in the DB.
Creates a response to send back to the client.
The server crashes before sending the response.
The client gets a timeout error (or connection refused?)
The client is bound to think that the contact creation has failed, though, in fact, the contact was in the DB.
Is this a rare case we can ignore? How do big companies deal with such an issue?
To handle this, you should make your write APIs idempotent i.e. If the same operation is executed multiple times, the result should be same as the operation was done only once.
To achieve this in your current example, you need to be able to identify a contact uniquely based on some parameter, say emailAddress. So, if the createContact is called again with the same emailAddress, check in the DB if a contact already exists with the emailAddress. If so, return the existing contact. Else, create a new contact with the emailAddress and return it.
Hope this helps.
If the request times out, the client should not make any assumption about whether it failed or succeeded.
If it is just a user making a request from a web form, then the timeout should just be exposed to the user, and they can hit the back button and check whether the operation succeeded or not, and if not they submit the request again. (This is fine as long as you always keep a consistent state. If your operation has multiple steps and fails mid way, you need to roll back.)
However if reliable messaging is important to your application you will have to use a library or build your own reliable messaging layer. This could work by having the client assign a unique ID to every request, and having another request that lets you check the result of that request ID later. Then you can do automated retries but only where necessary.

How to get last logout timestamp from self?

Is it possible for a XMPP client to get a timestamp indicating the last time when he got offline? Something like what's described on XEP-0012: Last Activity (this can't be done using this extension since the user should be offline in order for its last activity to be the offline presence stanza).
I am not sure what you want to do, but it is not possible currently in ejabberd (ejabberd implements the XEP-0012 Last Activity feature requests).