You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi, I'm trying to encode a model called Order that has a property called foods that's a dictionary with arrays as values: [FoodType : [AnyFood]]. The keys, FoodType, are enum values with String as their raw value. FoodType also conforms to Codable. When I encode an Order object though with FirebaseEncoder().encode(order) and then setValue(orderData), I get something weird in my Firebase Realtime Database. What I would expect is a child whose path would be /order/foods/salad with enumerated children for each value of the AnyFood array. What happens though is this:
It makes a child /foods with the first item in the foods dictionary has its key 'salad' as /0 and the array value for salad as /1 with the enumerated children as the array's values. How can I encode order in such a way where the value for the path /order/foods/salad/0 would be the first item in the salad key value's array. And then when I would have a coffee key it would map to /order/foods/coffee? (The FoodType keys should be children keys themselves in the database with there children being the values in the [AnyFood] array)
The text was updated successfully, but these errors were encountered:
That is the way Codable works when they key in the dictionary is enum. That is a swift's limitation for now. You will have to map it to [String: [AnyFood]] yourself. For more background refer to this page
So the results you got were expected. You need to manually map [FoodType : [AnyFood]] to [String: [AnyFood]] in func encode(to encoder: Encoder) throws and the [String: [AnyFood]] to [FoodType : [AnyFood]] in init(from decoder: Decoder) throws. It will not work automatically
Hi, I'm trying to encode a model called
Order
that has a property calledfoods
that's a dictionary with arrays as values:[FoodType : [AnyFood]]
. The keys,FoodType
, are enum values withString
as their raw value.FoodType
also conforms toCodable
. When I encode anOrder
object though withFirebaseEncoder().encode(order)
and thensetValue(orderData)
, I get something weird in my Firebase Realtime Database. What I would expect is a child whose path would be/order/foods/salad
with enumerated children for each value of theAnyFood
array. What happens though is this:It makes a child
/foods
with the first item in the foods dictionary has its key 'salad' as/0
and the array value forsalad
as/1
with the enumerated children as the array's values. How can I encodeorder
in such a way where the value for the path/order/foods/salad/0
would be the first item in thesalad
key value's array. And then when I would have acoffee
key it would map to/order/foods/coffee
? (TheFoodType
keys should be children keys themselves in the database with there children being the values in the[AnyFood]
array)The text was updated successfully, but these errors were encountered: