0

In Xamarin.Form Mobile App, is there anyway we can run functions in the background without stopping it when we move to another PageView? For example, I want to run this below C# code in my App for listening to Firebase Real-Time Database without stopping it when I move to another PageView to keep my data update nicely without having to start it again which will delay/slow my UI process:

var firebase = new FirebaseClient("");
var observable = firebase
.Child("RoomInfo")
.AsObservable<RoomModel>()
.Subscribe(d => getTotalMember = d.Object.TotalMember);
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Steven-Carrot
  • 2,368
  • 2
  • 12
  • 37

1 Answers1

1

You can use asynchronous function using async and await keywords. It will run your function, and allow other processes to continue before your function will return value. See here.

The exact implementation depends on the rest of your code and how you will structure it, but the concept should get you on the right track.

If it is bigger task, you can run it using Task.Run, which will execute your code independently from main thread (note: to update values on UI you will have to use Device.BeginInvokeOnMainThread, as Task.Run will change thread your code is executed on, while UI will be still executed on the main thread).

Adam Kaczmarski
  • 338
  • 1
  • 12