Closed analogcode closed 7 years ago
It seems to work if one removes a property from a struct. How cool would it be if it could load a struct that has since had properties added? And, those properties could just default to the struct's default values...
Hey @swiftcodex thank you so much and you're welcome!
Yes I've actually thought about this scenario a bit. At the end of the day, Disk just uses JSONEncoder
and JSONDecoder
to convert structs to and from JSON data, so beyond their capabilities, messing with the JSON data may result in unexpected behaviors. I think it goes a bit out of Disk's purpose to offer handling conversion of invalid typed JSON files to new struct types. Let's say we have a struct Message
, you save a message to the documents directory as message.json
and then you update the Message
model. Now you should first retrieve this outdated message data as Data
:
try Disk.retrieve("message.json", from: .documents, as: Data.self) // instead of Message.self
And use the JSONSerialization
class to manually retrieve your old message's properties and create a new instance of the new Message
struct and manipulate the old data however you need to.
This way makes sense, since it's a process that needs to be fine tuned to the developer's scenario. JSONEncoder
and JSONDecoder
should take care of most edge cases like you mentioned, and will get better with updates to Swift.
I hope I helped! Thanks again for the kind words :^)
Okay, cool. That sounds like a simple solution. Thanks! 👍🏼
@saoudrizwan Thank for your assistance, and the library. I explored it more this morning, and, it works like a charm. We are going to use it in the new open-source AudioKit Synth to handle presets! AudioKit Synth One
I'm glad to hear that @swiftcodex! I added Synth One to 'Apps Using Disk' on the README: https://github.com/saoudrizwan/Disk#apps-using-disk
If you run into any problems, let me know! :^)
Oh sweet- Thanks for the support!
@saoudrizwan Here's another app in the app store we made that uses Disk. https://audiokitpro.com/fm-player-classic-dx-released/
It has been getting rave reviews. Maybe thanks to Disk ;) https://ask.audio/articles/free-dx7-instrument-the-best-sounding-fm-synth-for-ipad
@swiftcodex Wow! Congratulations on your app's success so far man, great great work! And you have no idea how glad it makes me to see Disk being used in apps like these that people love using. I can really tell you put a lot of attention to detail and the interface looks gorgeous! I'm actually a big Kyle Dixon fan so I love 80s synths haha. I just added this to the README too, happy coding!
@saoudrizwan - Thank you and awesome! I actually have a few Kyle Dixon / Stranger Things inspired patches I made in the FM Player. Nice reference!
@saoudrizwan - I have just quietly pushed the source code. It has a link back to Disk - https://github.com/AudioKit/ROMPlayer
I read about Disk in your README, I'm honored! Thank you so much for the credit!
This library is awesome- lightweight, fun, and useful. Thank you for making this!
Have you thought about any kind of data migration? For example, if someone adds/removes a property to a struct after saving it, and then tries to load a version of a json file saved using the old version of the stuct?
Guessing you probably don't want to build something as robust as Realm's schema migration... have you brainstormed ideas on the best way to implement this?
Thanks again!