HandleChangeAsync...stream...throttle? :-)


#1

Ciao and thank you very much in advance!
The INotificationHandler and the relative HandleChangeAsync method are super beautiful :smile:
And for sure comfortable in case of “insertion” changes.
But…in case of “modification” changes…there is a way to throttle the “stream”?
It would be very very beautiful if here we can have something like an Observable :smile: with the possibility to use reactivex (with the operators :heart_eyes:) to manage the stream.
In my case I try to do something like the nice Feedback provided by Nikola…but listening to modifications and and not insertions. So I want to call an Azure Function not for each modification but only after some time.
Something exactly like the Throttle operator.
In any case…nice job!


#2

Right now, there isn’t a way to elegantly control that. However, you should be able to add await Task.Delay(1000) in your HandleChangeAsync to apply a 1 second throttle. Then the next invocation will contain an aggregation of the changes that happened over that second to that Realm. Keep in mind that this poor-man’s throttling will only affect changes happening on the same Realm.


#3

Thanks Nikola :slight_smile: it is exactly what I’m doing now! :grin:


#4

unfortunately using Task.Delay() all the calls to HandleChangeAsync are simple delayed…but not throttled. I try to do something like 1 call now, the next after 3 seconds…all the calls during the throttle interval they must be discarded :sweat_smile:


#5

What do you mean discarded? If I’m understanding you correctly, that should not be the case. E.g. if a change occurs while a previous change is being handled (which now happens slowly due to Task.Delay), you’ll still get a notification for it later on. But keep in mind that the changes will get aggregated, so if you do something like:

realm.Write(() =>
{
    myObj.Foo = 1;
});

// wait a few milliseconds
realm.Write(() =>
{
    myObj.Foo = 2;
});

You’ll get a single notification that will tell you that myObj has changed and the new value is 2.


#6

ok to be able to figure out my example is more or less the same as the one inside your Feedback repo in github. But in my case I’m interested in Modifications and non Insertions. And Modifcations in this case it means that I’m editing a text inside a Xamarin Forms Listview cell. So every time I insert (or delete) a new char the handler is triggered…but I want to call an Azure Function only when I finished to write. I know this is almost impossibile (or very difficult) without an rx stream. So if i put a Task.Delay inside the handle the IChangeDetail passed in as a parameter is the old one (the one before the delay) or not after the delay? and in any case how I can discard all the other enqued calls triggered during the delay?
Another idea it can be: use a reactivex Subject and call OnNext() every time the handler is triggered but then during the Subscribe handler there is a thread problem with the beautiful well known exception when a realm object does not belong to the correct thread :grin: