Forum Xamarin.Android
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Retrieving an array from firebase using Xamarin Android

In firebase database, I have babysitters, which are made up of strings and an array, availability this is what it looks like in firebase:

{"babysitter": {
{"-UserID" : {
"Address": " djsh",
"Age": "26",
"Availability": [
0: "Monday Morning",
12: "Saturday Afternoon",
18: "Friday Evening"
19: "Saturday Evening"
27: "Sunday Night" ],

 "City": "hjhj",
 "Eircode": "djsahdja",
 "Name": "jenna",
 "Email": "[email protected]"
},

{"-UserID": {
"Address": " kokjk",
"Age": "29",
"Availability": [
0: "Monday Night",
12: "Tuesday Afternoon",
18: "Friday Evening"
19: "Saturday Morning"
27: "Sunday Night" ],

 "City": "kjdsakhd",
 "Eircode": "nkkj7",
 "Name": "paul",
 "Email": "[email protected]"
}

}
}
I could retrieve all the data , when it was just strings, before I started storing an array in firebase using this code in Xamarin Android Visual Studio 2017:

            var items = await firebase
                    .Child("babysitter")
                    .OnceAsync<BabySitter>();
            list_babySitters.Clear();
            babysitterAdapter = null;
            foreach (var item in items)
            {
                BabySitter account = new BabySitter();
                account.id = item.Key;
                account.name = item.Object.name;
                account.age = item.Object.age;
                account.phone = item.Object.phone;
                account.city = item.Object.city;
                account.address = item.Object.address;
                account.email = item.Object.email;
                account.eircode = item.Object.eircode;
                account.availability = item.Object.availability;
                list_babySitters.Add(account);

            }

Since I started storing an array in firebase, I am unable to run the application without it crashing at this point in the code :

var items = await firebase
.Child("babysitter")
.OnceAsync();
I have tried the forums and have not been able to find anyone with a similar problem and solution using Xamarin Android Visual Studio 2017. Any help of advice will be greatly appreciated, thanks.

Answers

  • LandLuLandLu Member, Xamarin Team Xamurai

    What does your model class BabySitter look like? If its structure is different from your json string, this error could raise up. You can try this deserialize method:
    var items = await firebase.Child("babysitter").OnceAsync<object>();
    Will it crash too?

  • JamesMJamesM Member ✭✭

    public class BabySitter
    {
    public string id { get; set; }
    public int age { get; set; }
    public string name { get; set; }
    public string email { get; set; }
    public string phone { get; set; }
    public string address { get; set; }
    public string city { get; set; }
    public string eircode { get; set; }
    public string[] availability { get; set; }
    }

  • LandLuLandLu Member, Xamarin Team Xamurai

    @JamesM The Availability is not a string array from your strings It could be the issue. And try to keep your model properties' names the same as the strings come from firebase. Please note that they are case sensitive.
    Have you tried var items = await firebase.Child("babysitter").OnceAsync<object>();? If it works fine it means your model's structure causes the issue.

  • JamesMJamesM Member ✭✭

    Hi @LandLu , the model properties are the same as they are in firebase, all lowercase (availability). I tried this approach:

        var items = await firebase.Child("babysitter").OnceAsync<object>();
                list_babySitters.Clear();
                babysitterAdapter = null;
    
                foreach (var item in items)
        {
            babySitter = JsonConvert.DeserializeObject<BabySitter>(item.Object.ToString());
            list_babySitters.Add(babySitter);
        }
    

    All the Babysitters who have null availability works fine, but as soon as it iterates to the babysitter with availability, it crashes.

  • JamesMJamesM Member ✭✭

    The two screenshots above are of babysitters, the top screenshot the babysitter has availability of monday morning, tuesday morning, monday evening and thursday evening. The bottom screenshot has a babysitter with no availability set, when i'm debugging i iterate through all the babysitters no problem, showing availability as Null. As soon as i come to babysitter : LYgS9-RaGasiDaeYMso the program crashes. Thanks for any help you can give.

Sign In or Register to comment.