Using Reactive Extensions to create a single event? - system.reactive

I'm trying to get my head around .NET Reactive Extensions, and I wonder if they can be used in the follow scenario:
In my WP7 app, I'm using the SterlingDatabase to persist app settings. As a user is modifying the settings, I want to periodically call Database.Flush()
So in my Property Set method, I'd like to kick off a Database.Flush() timer event, and in say 5 seconds, write to the database. If another property is written to, I want to restart the timer.
I know I can do this with a timer object, calling Start() and Stop(), but I wanted to know if I could do this with Rx, to create an Asycn operation that I can basically start and stop, without using a timer?

Use Throttle:
public void AttachFlushes(IObservable<Unit> writes, SterlingDb db)
{
writes.Throttle(TimeSpan.FromSeconds(5)).Do(_ => db.Flush()).Subscribe();
}

Related

How to subscribe to events in OnEnable when the object that is subscribed to is initialized in Start?

When using (C#) events in Unity, OnEnable and OnDisable are used to subscribe and unsubscribe to the events, so callbacks won't be triggered on disabled objects. However, sometimes the object that is subscribed to can be initialized only at Start (that is called after OnEnable) which means that when OnEnable is called for the first time, you cannot subscribe to the object as it's not initialized yet (later OnEnables will work fine). One fix for that, as also described here, is to use a flag "hasStarted", so the first time you will subscribe at Start and set it to true, but any other time the object is enabled you will subscribe using OnEnable (making sure that hasStarted is true, that is, it's not the first OnEnable call).
However, that seems really ugly, especially since this situation is not rare at all. And I wonder if there are any other ways to get such a functionality without using a one-time flag.
The methods go Awake->OnEnable->Start so you can do the event subscription inside OnEnable only. It's a bit convoluted to my liking as well, but you won't need any additional flags.
Just tested it for UI Toolkit because I had a similar issue (I was subscribing in Awake for button clicks so when setting back to active I had no more event subscriptions since Awake wasn't called again and OnDisabled unsubscribed automatically). (MainMenu->Settings->MainMenu situation).

Swift - Automatically trigger functions at particular times

I am using Swift as a programming language and Firebase for my backend. My use case is about creating an event (a tournament) which has different rounds (RoundOf16, Quarter-Finale, Semi-Finale etc.)
Whenever a new rounds starts and finishes, I want to send out notifications to the participants. It would be easy to do that by using local notifications.
However, additionally to sending the notifications, I also want to trigger a function to check if a participant can make it to the next round (if a round is finished) or if the event has even enough participants to start.
How can I do that? Is there a client-side way to achieve this or can it only be done server-side? The functions would need to run completely in the background as it is the case that you close the app, even though the round is still active.
We have to consider that multiple tournaments are going to be created. This means, for each tournament, we have couple of rounds with a beginning and ending date.
What would be the most elegant way? Having one function where I pass the tournamentId so based on that, I can check who won the round? But wouldn't I overwrite the function to be triggered every time then?

Is using Firestore Cloud functions different from using active listeners within the application?

I've been trying to get an answer to my issue for weeks now without any luck.
I have an app in swift that displays a few labels on the screen. I want the label text to change everyday at midnight, and I want to be able to decide what the label should say from my machine.
I've been told to use either active listeners or cloud functions.
The video below shows a dev using firebase cloud functions to alter text in real time on an app.
https://www.youtube.com/watch?v=Z87OZtIYC_0
How is this any different from active listeners which also do the same?
Objectively speaking, what suits my use case more?
To execute a function at midnight to make a UI change first requires that the app is open. If the app isn't even open then there isn't a UI to update. Therefore, there is no sense in tasking the server with this function because the app may not even be open. And the task of updating the UI should be the responsibility of the client, anyway.
Therefore, create a timer on the client (in your app) that makes a call every midnight to get data from the server that populates the labels. What you need is to create a Timer object that fires in n seconds to midnight that has an interval of 1 day that repeats infinitely. It may look something like this:
var midnightTimer: Timer?
midnightTimer = Timer(fire: Date().addingTimeInterval(secondsToMidnight), interval: oneDayInSeconds, repeats: true) { (timer) in
self.updateLabels()
}
RunLoop.main.add(midnightTimer!, forMode: .common)

How to add an Event object to Unity's EventSystem

If I have an event from Event.current and I want to 'replay' it by making the event system process it again, how do I do that? Can I access the scripts responsible for raising events?
You might be able to track down how events are created using IL Spy then use reflection to invoke that, but unfortunately there is no public API for creating events manually.
I found a neat C# library which simulates input called Windows Input Simulator which does a good job of raising all the keyboard events I needed. Looks like it also works for things like mouse input!

Is there a sendToActivity() method?

I have a program with about 8 Activity classes, and 1 Application class. I want my Application class to be able to communicate with every Activity, but on its own terms. I don't want the activity to ask the Application for data, I want the Application to send the Activity data. The problem with this, is that depending on the current state of the program I'm unsure what Activity will be open.
Is there a method of some sort which will send information from the Application to the CURRENT activity?
The Application class connects with an embedded Bluetooth Device and needs to receive different pieces of data depending on which Activity the user is currently in. I originally had it as a regular class, which was initialized in the MainMenu of my program and passed a Handler. However, it seemed like weak design to pass that Handler from Activity to Activity time and time again.
You could use a Callback Method
Every Activity has it's own callback method and registers that method onResume() in the Application Class. (it's like an onApplicationWantsToDoSomethingWithMeListener() ;)
or why not a Service in background? instead of the Application, since what you want sounds like a Service. More details?
EDIT:
I made a similar application with bluetooth, you should definetly use a Service for that, but you can communicate with your service per Application. Say the Service calls the callback in the Application look here for an implementation uf such a thing