saoudrizwan / Disk

Delightful framework for iOS to easily persist structs, images, and data

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Crash: Adding a new variable is causing.

hemangshah opened this issue · comments

Hi @saoudrizwan , I am a long time user of this library! Thank you. ✌️Today, I came across a crash is happening when I will add a new variable to my struct.

Example:

  1. I need to create a struct named: Person. I will store name and age details of a person.
struct Person: Codable {
   var name: String = ""
   var age: Int = 30
}
  1. Now, I will create few objects of Person and will save it using Disk.

  2. Later, I have a new requirements and I need to add one more variable to store a person's gender. So I'll update my struct like this:

struct Person: Codable {
   var name: String = ""
   var age: Int = 30
   var gender: String = "M"
}
  1. Now, I will run the app. This will cause a crash.

The only solution is to empty Person.json file and run the app again.

Anyways to fix this?

To avoid crash, we can use do { try? } catch { ... } block but it will also goto catch block, how to fix it without it?

Hi @hemangshah, the crash looks to be happening because you're using a force try (try!) when trying to decode JSON with an updated struct model. In other words, your JSON looks like this

name: "Saoud"
age: 100

and you're trying to decode a 'gender' variable from it after you update the model and tell disk to retrieve this outdated JSON with your new model.

This is where the practice of migration comes in. When you update a data model and know that there is data out there being used with the old model, you need to set up migration.
So for example, when your app first launches, retrieve all the Person objects in their original model form, i.e. as Data.self and manually pull out their properties using JSONSerialization, then create new Person objects using these old properties, with a default value for gender.
Or you could make gender optional in your new model, so when Disk tries to decode older models without gender, it returns nil.

Hi @saoudrizwan , thank you - there are people who might have extensively using Disk in their project. Can't you develop something which will not requires this migration and will work smoothly?

* develop = some code changes in your code which handle this model change easily? If all the developers will requires to do this migration then its going to be a lots of work for us. Can't we fix this at one place only?

You don’t have to do that migration if you make your model’s new properties optional. It doesn’t make sense to be able to decode non-optional properties from JSON that doesn’t have the data attributes you expect.

Aah, this seems to be a good point but what if we don't have to keep that property an optional. May be you can return an empty string for the String type or like wise? Later, we can change it as per the needs.