alickbass / CodableFirebase

Use Codable with Firebase

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

backward compatible options

ricocheung opened this issue · comments

hi there,
This is quite handy! thanks for sharing.

A question, the decoder throws when Firestore record doesn't exactly match with Class property definition, anyone has recommendation how to handle adding or removing fields between client versions?

for example, myApp version 1 stores object "People" with 2 fields "NameFirst" and "NameLast", using encode and decode are working great.

when i'm doing myApp version 2, when I add a new propoery "PhoneNumber" to People class, the decode throws and return nil, as the new field PhoneNumber doesn't exist in other existing records.

what are the consideration when we decided to throw, rathan than just return nil to the fields that doesn't exist?

thansk!

guard let entry = self.container[key.stringValue] else {
            throw DecodingError.keyNotFound(key, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "No value associated with key \(key) (\"\(key.stringValue)\")."))
        }

Well, in general having extra data in the model will not cause the error, it's only the missing one. So if you have different version of the app, you need to make sure that they only add new data not delete anything. This way you will never have any errors.