Reputation: 714
My query returns a list of FirebaseObjects:
IReadOnlyCollection<Firebase.Xamarin.Database.FirebaseObject<object>> items = await firebase.Child("product").OnceAsync<object>();
But I want to do this:
Product product = await firebase.Child("product").OnceAsync<Product>();
Can I change my query to make it work, or is this just how it works? What I mean is, will I always get back a list or can I deserialize directly to my Product object?
Full structure of json
{
"product1": {
"pname": "Peppermint Kiss",
"teaType": "Black Tea",
"company": "PersnickeTea",
"packSize": {
"teabag": [
{
"qty": 1,
"price": 1.5,
"tImage": "singleCup.jpg"
},
{
"qty": 5,
"tinPrice": 6.95,
"tImage": "blackTeaTin.jpg"
},
{
"qty": 8,
"price": 10,
"tImage": "blackTeabox.jpg"
},
{
"qty": 12,
"boxPrice": 14.95,
"tImage": "trayBox.jpg"
}
],
"looseLeaf": [
{
"qty": 46,
"price": 7.95,
"tImage": "mintKissPak.jpg"
}
]
},
"ingredients": [
"black tea",
"peppermint leaves",
"candy pieces",
"organic peppermint oil",
"natural creme flavor",
"raspberry leaves",
"red clover",
"anise seed"
],
"prepMethod": [
"infuser",
"teabags",
"strainer",
"iced"
]
},
"product2": {
"pname": "Spearmint Kiss",
"teaType": "Black Tea",
"company": "PersnickeTea",
"packSize": {
"teabag": [
{
"qty": 1,
"price": 1.5,
"tImage": "singleCup.jpg"
},
{
"qty": 5,
"tinPrice": 6.95,
"tImage": "blackTeaTin.jpg"
},
{
"qty": 8,
"price": 10,
"tImage": "blackTeabox.jpg"
},
{
"qty": 12,
"boxPrice": 14.95,
"tImage": "trayBox.jpg"
}
],
"looseLeaf": [
{
"qty": 46,
"price": 7.95,
"tImage": "mintKissPak.jpg"
}
]
},
"ingredients": [
"black tea",
"peppermint leaves",
"candy pieces",
"organic peppermint oil",
"natural creme flavor",
"raspberry leaves",
"red clover",
"anise seed"
],
"prepMethod": [
"infuser",
"teabags",
"strainer",
"iced"
]
},
"product3": {
"pname": "Mintymint Kiss",
"teaType": "Black Tea",
"company": "PersnickeTea",
"packSize": {
"teabag": [
{
"qty": 1,
"price": 1.5,
"tImage": "singleCup.jpg"
},
{
"qty": 5,
"tinPrice": 6.95,
"tImage": "blackTeaTin.jpg"
},
{
"qty": 8,
"price": 10,
"tImage": "blackTeabox.jpg"
},
{
"qty": 12,
"boxPrice": 14.95,
"tImage": "trayBox.jpg"
}
],
"looseLeaf": [
{
"qty": 46,
"price": 7.95,
"tImage": "mintKissPak.jpg"
}
]
},
"ingredients": [
"black tea",
"peppermint leaves",
"candy pieces",
"organic peppermint oil",
"natural creme flavor",
"raspberry leaves",
"red clover",
"anise seed"
],
"prepMethod": [
"infuser",
"teabags",
"strainer",
"iced"
]
}
}
Upvotes: 0
Views: 648
Reputation: 714
I was able to get a solution that works for me.
var items = await firebase.Child("products").OnceAsync<object>();
foreach( var item in items) {
product = Newtonsoft.Json.JsonConvert.DeserializeObject<Product>.(item.Object.ToString());
}
But I bumped up apineda's solution. The query works but throws an exception when I try to use the product.
var product = await firebase
.Child("products")
.Child("product1")
.OnceAsync<Product>();
But I think I would need to make x # firebase calls to get all while my solution gets all products in one call.
Upvotes: 0
Reputation: 9356
Your query returns a list of object because you are querying a main node without adding any filter. That's "the same" as saying
SELECT * FROM PRODUCT;
in a relaccional database.
You cannot do
Product product = await firebase.Child("product").OnceAsync<Product>();
when you have a list of products comming from the request. If you want to pick only one then you need to filter either by key, by child or by value.
But you can though do:
var products = await firebase.Child("product").OnceAsync<Product>();
And you will have a collection of products, already deserialized for you.
Use this:
var product = await firebase
.Child("product")
.Child("your-product-id")
.OnceAsync<Product>();
To get a single product if you defined your productId to be the key of your node.
Moved structure to original post....
Upvotes: 1