C# FirebaseDataBase API - Real Time Get Data into a List
See original GitHub issueI´m new in this topic about Firebase in C#, the online documetation is bad, I only find 2 (FireSharp and FireBaseDataBase) API for connect my Desktop App with Firebase and also I try to connect my app with JSON HttpRequest. In all the case when i try to PUT, UPDATE, DELETE, the response time delay 4 seconds and i´m sure that my internet have a good Connection. On the other hand and as a main question (using the API FirebaseDataBase for C# WPF) is why i can´t put the real time data in a List(). First I try to do this.
//Example//`
public void StreamData()
{
List group= new List();
var firebase = new FirebaseClient("https://dinosaur-facts.firebaseio.com/");
var observable = firebase
.Child("dinosaurs")
.AsObservable()
.Subscribe(d => group.Add(d.Object));
}
But the problem here is that “group” dont Add “d.Object”. Then I try to use this code:
public void StreamData()
{
var observable = firebaseClient.Child("Information").AsObservable<Persona>();
var t= observable.ObserveOnDispatcher().AsObservableCollection();
}
But I have this problem :
System.InvalidOperationException: ‘The current thread has no Dispatcher associated with it.’
In summary i try to get a real time data from Firebase with the API FireBaseDataBase, as a List() because i want to insert in my Datagrid.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:1
- Comments:15 (1 by maintainers)
Top GitHub Comments
I’m pretty new to Xamarin and definitely to FirebaseDatabase.net API, but have figured it out. Here’s all the details on how I got this to work, including getting ListViews to be automatically updated from a Firebase subscription. Warning - this is a pretty long post. I’ve tried to provide ALL the details. Again - I’m new to C# and Xamarin, so I may not refer to things the “proper” way. Hopefully, I haven’t left out anything important. I’ll monitor this thread to see if I can help if you have questions/problems. Also, I hope I don’t offend everyone with my basic explanations - you probably already know most of the core stuff here.
Overview of the process:
Now for all the code - just in case you can’t follow what I just typed.
In my Helpers.cs file:
In EventClass.cs: My sample object class using INotifyPropertyChanged - this allows binding and the list view will stay current
Somewhere in your app, you need to authorize the user (login) to the database. In my case, I’ve included try/catch in order to display error messages to the user
Somewhere in your app, you will need to start the subscription to the database I experienced a performance problem (30 second delay for the first pull of data) if I started this as a subscription. So, instead I pull the data one time (almost instantly), and then start the subscription:
Almost there! At this point, the ObservableCollection has your data from the database and will be kept in sync now you just need to connect that ObservableCollection to your ListView.
On your xaml page, create your ListView, binding to the properties of your class:
FINALLY - on your xaml.cs code behind page, set the ListView’s ItemsSource to the ObservableCollection:
Let the questions begin!
@fabricciotc Hey I tried your method but I am getting this error: “The current thread has no Dispatcher associated with it”, also I have tried what @TennisDrum posted but my DataGrid does not updates, although the changes in the Firebase are detected by .Subscribe(), my ObserVableCollection remains the same as when I first retrieve the data