locked
Firebase database query RRS feed

  • Question

  • User131807 posted

    Hi

    I want to query from firebase database but I couldn't find a suitable query method. Here a javascript example exists http://jsfiddle.net/katowulf/kyu2u/ but is there a similar method in the Xamarin.Firebase database?

    Thanks

    Monday, October 10, 2016 11:14 AM

Answers

  • User250249 posted

    Hello,

    I am not familiar with Xamarin.Firebase, but there is another package I have been using without issues.

    https://www.nuget.org/packages/Firebase.Xamarin

    public async Task GetFromDatabase() {
                var firebase = new FirebaseClient("https://<your-firebase-address>.firebaseio.com/");
    
                var items = await firebase
                  .Child("<table_name>")
                  .OrderByKey()
                  .WithAuth(FirebaseAuth)
                  .OnceAsync<YourClass>();
    
        //Do whatever you need with items such as
    
        foreach (var item in items) {
            //Access values by item.Object.<field_name> and item.Key
                }
    
    }
    
    public class YourClass {
            //Database fields here
    }
    
    public static string FirebaseAuth() {
    
            var tokenGenerator = new TokenGenerator("your firebase key");
            var authPayload = new Dictionary<string, object>()
            {
              { "uid", "1" },
              { "some", "arbitrary" },
              { "data", "here" }
            };
            return tokenGenerator.CreateToken(authPayload);
        }
    
    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Monday, October 10, 2016 11:41 AM

All replies

  • User250249 posted

    Hello,

    I am not familiar with Xamarin.Firebase, but there is another package I have been using without issues.

    https://www.nuget.org/packages/Firebase.Xamarin

    public async Task GetFromDatabase() {
                var firebase = new FirebaseClient("https://<your-firebase-address>.firebaseio.com/");
    
                var items = await firebase
                  .Child("<table_name>")
                  .OrderByKey()
                  .WithAuth(FirebaseAuth)
                  .OnceAsync<YourClass>();
    
        //Do whatever you need with items such as
    
        foreach (var item in items) {
            //Access values by item.Object.<field_name> and item.Key
                }
    
    }
    
    public class YourClass {
            //Database fields here
    }
    
    public static string FirebaseAuth() {
    
            var tokenGenerator = new TokenGenerator("your firebase key");
            var authPayload = new Dictionary<string, object>()
            {
              { "uid", "1" },
              { "some", "arbitrary" },
              { "data", "here" }
            };
            return tokenGenerator.CreateToken(authPayload);
        }
    
    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Monday, October 10, 2016 11:41 AM
  • User131807 posted

    Thank you, I think Xamarin.FireBase.Database didn't implement this query interface yet.

    Monday, October 10, 2016 11:45 AM
  • User131807 posted

    Hello again,

    Well I have just started using firebase database recently. I am not sure what I am missing but I can't get a result set. My structure on firebase is like below. I made a simple sampling to test it:

    myproject ---myusers ------149AD97ACF7DC520 ---------id: 1

    Function is like below:

    `public static async System.Threading.Tasks.Task GetFromDatabase() { var firebase = new FirebaseClient("https://myproject.firebaseio.com/myusers");

    var items = await firebase
      .Child("149AD97ACF7DC520")
      .OrderByKey()
      //.WithAuth(FirebaseAuth)
      .LimitToFirst(1)
      .OnceAsync<MyClass>();
    
    foreach(var item in items)
    {
        GameDataManager.id = item.Object.id;
    }
    

    }`

    I call this method in a static method like below:

    System.Threading.Tasks.Task.Run(() => { GetFromDatabase().ConfigureAwait(false); });

    And MyClass is like below:

    public class BData { public string id; }

    Monday, October 10, 2016 6:12 PM
  • User84670 posted

    You should await your call to GetFromDatabase().

    System.Threading.Tasks.Task.Run(async () => { await GetFromDatabase().ConfigureAwait(false); });

    Monday, October 10, 2016 8:28 PM
  • User250249 posted

    I'm not certain but you may need to call Childtwice.

    var items = await firebase
      .Child("myusers")
      .Child("149AD97ACF7DC520")
      .OrderByKey()
      //.WithAuth(FirebaseAuth)
      .LimitToFirst(1)
      .OnceAsync<MyClass>();
    
    foreach(var item in items)
    {
        GameDataManager.id = item.Object.id;
    }
    
    Tuesday, October 11, 2016 10:12 AM
  • User131807 posted

    I tried several times but I couldn't get this query run, maybe it stuck at awaiting or somewhere else in the library. But I found another library working, at least for my case: https://github.com/step-up-labs/firebase-database-dotnet Thanks again for your help guys.

    Thursday, October 13, 2016 8:52 AM
  • User269082 posted

    HI, Could anyone give me a sample for sending data to firebaseDB?

    var item = await firebase .Child("users") .Child("username") .WithAuth(auth.FirebaseToken) .PutAsync(userJson);

    Here how to send userJson? i want to send username, password and created_time as the json.

    I need help in creating the object userJson

    Monday, November 7, 2016 2:57 PM
  • User131807 posted

    Hello,

    I don't know which library you used but I use firebasedatabaseDotNet library. If you go with that here is what you can do:

    I assume your data structure is like below:

    myproject.firebaseio.com ---users ------"user1" ---------username ---------password ---------createdtime

    For such generic structures containing standard variable names in firebase you can define a generic class like this:

    public class MyUser { public string username; public string password; public DateTime createdtime; }

    Then you set the information of current user like this:

    MyUser myUserData = new MyUser(); myUserData.username = "user1"; myUserData.password = "pswd1"; myUserData.createdtime = now;

    and then simply call this:

    await this.firebase .Child("users") .Child(myUserData.username) .WithAuth(auth.FirebaseToken) .PutAsync<MyUsers>(myUserData);

    You can also use this custom class for querying from firebase, it makes easier for such objects.

    If you just update only one object in under this user you can do it like this (for instance you'll update the password):

    await this.firebase .Child("users") .Child(myUserData.username) .Child("password") .WithAuth(auth.FirebaseToken) .PutAsync<string>("newPassword");

    Monday, November 7, 2016 8:07 PM
  • User131807 posted

    I also suggest you to review the firebase' (google's) suggestions about structuring the data you'll use for your application. Sometimes you may hold unnecessary data or sometime you don't if you need indexing in an another object.

    Monday, November 7, 2016 8:12 PM
  • User269082 posted

    Thank you very much ashmez. It worked perfectly. I was struck with converting the class to json and send. Thanks again.

    Tuesday, November 8, 2016 9:39 AM
  • User279438 posted

    Hello everyone my database is stored like this--

    "MyMeetings" : { "-KjSIiepWuIbXLLVHN" : { "AttendeesName" : "Shreya,Pelak", "MeetingCompany" : "Goldman Sachs", "MeetingDate" : "Tue,May 9 2017", "MeetingTime" : "10:00 PM", "MeetingTitle" : "Create an Interest" }, "-KjSZdbgilIP5ZGAW1vS" : { "AttendeesName" : "Abc,Cyan", ""MyMeetings" : { "-KjSIiepWuIbXLLVHN" : { "AttendeesName" : "Shreya,Pelak", "MeetingCompany" : "Goldman Sachs", "MeetingDate" : "Tue,May 9 2017", "MeetingTime" : "10:00 PM", "MeetingTitle" : "Create an Interest" }, "-KjSZdbgilIP5ZGAW1vS" : { "AttendeesName" : "Abc,Cyan", "MeetingCompany" : "Citigroup", "MeetingDate" : "Thu,May 11 2017", "MeetingTime" : "12:00 AM", "MeetingTitle" : "Create an Interest" }" : "Citigroup", "MeetingDate" : "Thu,May 11 2017", "MeetingTime" : "12:00 AM", "MeetingTitle" : "Create an Interest" }

    I want to retrive MyMeetings sorted by MeetingDate. How to do that?

    I am using Firebase.Xamarin by rlamasb.

    Currently i am using this to retrive all list --

    var Meetings = await firebase .Child("MyMeetings") .OnceAsync();

    And doing sorting locally. Is there any way to do it in query.

    Friday, May 12, 2017 11:46 AM
  • User382371 posted

    Do you have any idea on how can I fixed firebase.xamarin is not compatible with Monoandroid81. I need to install it on my project.

    Monday, March 4, 2019 4:54 AM