Closed lacyrhoades closed 6 years ago
Ideally not only would the developer be able to "grow" the schema to allow for new properties, but also if an old property were to be removed, there could be some opportunity to fetch that old data from the file storage (just that one last time) and use the value to inform the model with the new schema.
In the event a user is upgrading directly from the first version of the schema directly to the 10th iteration, maybe it behaves opportunistically. If it can make the model object whole considering default values, it makes it, but then also it just fails pretty easily in the event something doesn't make sense.
Just saw #6 – Thanks!
Finally was forced to cross this bridge. Still using it for storing singletons. Ended up editing Persistable protocol to have a method init?(_: [String: Any])
as in https://gist.github.com/lacyrhoades/0794ac254620f6133f18649fce42cd02 so that, if a model needs to support upgrading/downgrading and the data doesn't match 100% it can implement that fail-able initializer.
OK scratch that, I was able to do what I wanted by just overriding public init(from decoder: Decoder) throws { }
and it's much shorter.
If a type has migrations, override that and handle custom decoding. If not, don't.
Say we start with some model user:
And later we add a property called like
birthdate: Date
In Disk, we are faced with two choices... We can omit
birthdate
from the CodingKeys enum forever and provide a default value for all user's birthdates (not ideal), or we can addbirthdate
to the keys and lose all previously storedUser
files (also not ideal).Is there some way to get the NSKeyArchiver to fall back and not abort altogether? Like, if in the event that the model DOES provide a default value for a property, it could maybe not absolutely require all of the CodingKeys.
Use case: I use Disk with this protocol to quickly store singleton objects with settings/preferences on them. Currently, if I "add" a preference property to one of these singletons, all of the previous settings get wiped out. Just curious if this is a reasonable feature in the scope of Disk.