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
First of all, thank you very much how this super useful library, I've been using it a lot and I really like it 👍
I'm not sure this is an actual issue or it's the intended behavior, but I'm having issues encoding models with optional values into data that I can use to update child values in the Realtime Database.
See the following model:
struct TestModel: Codable {
let id: String
let optionalField: String?
}
This is how I would encode it:
let model = TestModel.init(id: "123", optionalField: nil)
let encoder = FirebaseEncoder()
let data = try! encoder.encode(model)
When I inspect data, I see the following:
po data
▿ 1 element
▿ 0 : 2 elements
- key : id
- value : 123
It's seen that the key-value pair for the optionalField isn't included in the result
Here I would expect it to be encoded into
let data: [String: Any] = [
"id": "123",
"optionalField": NSNull()
]
/*
po data
▿ 2 elements
▿ 0 : 2 elements
- key : "optionalField"
- value : <null>
▿ 1 : 2 elements
- key : "id"
- value : "123"
*/
Lets say I have the following data in the realtime Database
/testData
/123
/id: "123"
/optionalField: "abc"
And I then want to update that model to at some point, with a an instance of the model that has optionalField as nil. I would do it like this:
let path = Database.database().reference(withPath: "testData/123")
let model = TestModel.init(id: "1234", optionalField: nil)
let encoder = FirebaseEncoder()
let data = try! encoder.encode(model)
let dataDict = data as! [String: Any]
path.updateChildValues(dataDict)
In this case the nothing is written to optionalField, because the FirebaseEncoder ignores that value when it's nil.
As mentioned, I'm not sure if this is the intent behaviour or a bug. On one hand, this is the same behavior the JSONEncoder has it also ignores nil-values. On the other hand it would be handy to have nil-value converted into NSNull, since this is what Firebase uses to delete values, and when inspecting the source code for _FirebaseEncoder, it seems to me that this is in fact the intended behaviour.
For now, I can work around this with using the setValue function on the DatabaseReference instead of the updateChildValues, but it sure would be handy for me if this optional value in fact would be encoded into NSNull.
The text was updated successfully, but these errors were encountered:
First of all, thank you very much how this super useful library, I've been using it a lot and I really like it 👍
I'm not sure this is an actual issue or it's the intended behavior, but I'm having issues encoding models with optional values into data that I can use to update child values in the Realtime Database.
See the following model:
This is how I would encode it:
When I inspect
data
, I see the following:It's seen that the key-value pair for the
optionalField
isn't included in the resultHere I would expect it to be encoded into
Lets say I have the following data in the realtime Database
And I then want to update that model to at some point, with a an instance of the model that has
optionalField
as nil. I would do it like this:In this case the nothing is written to
optionalField
, because the FirebaseEncoder ignores that value when it's nil.As mentioned, I'm not sure if this is the intent behaviour or a bug. On one hand, this is the same behavior the
JSONEncoder
has it also ignoresnil
-values. On the other hand it would be handy to havenil
-value converted intoNSNull
, since this is what Firebase uses to delete values, and when inspecting the source code for_FirebaseEncoder
, it seems to me that this is in fact the intended behaviour.For now, I can work around this with using the
setValue
function on theDatabaseReference
instead of theupdateChildValues
, but it sure would be handy for me if this optional value in fact would be encoded intoNSNull
.The text was updated successfully, but these errors were encountered: