Ok so I was looking for a way to store data to persistent storage in my app. Since I am building an HTML5 app, my first solution was to store data using the now deprecated WebSql database (indexedDB was only introduced in iOS 8!).  Now I had the basic data storage going and then I realised some of the storage limitations in the WebSql database, so I began searching for another solution and during that search I met Core Data!!! It was almost like love at first sight, it had all the storage awesomeness that I was after. Since there was so much love, I just wanted to build something and see how it works. In this post, I will talk about the random app crashes that happened on adding new entity to core data model and how to fix those.

Adding new entity to core data model

So I followed an online tutorial rather than reading Apple’s documentation on it, what I did was, I added an entity to my core data model and wrote the logic to store data and it was all working! So now my app could store and retrieve data from a persistent data source without the storage limitation imposed by WebSql in a webView. It was all going great and thanks to Core Data both my app and I were in a land of blue skies and sunshine 🙂 Now that I had something that saved and retrieved with Core Data, I felt I should refine my solution and actually add all the entities that my app needs. So I added a few entities to my data model and re-deployed my app to the device and BANG!!!

Until…a problem surfaced

The app crashed and I was no longer in the land of blue skies and sunshine. Why? In a nutshell, I needed to pay attention to what is described in Core Data Model Versioning and Data Migration. The solution is knowing all about what version of your Core Data model is your app dealing with. Or more like what your app knows it’s dealing with. Say you deploy version one of your app with entities Person and Vehicle, and then you add another entity called Animal and re-deploy your app to a device it will crash. Since the app is not expecting to find an Animal i.e. Animal entity. At this point if your delete I have written a more detailed post on the solution to this problem and you can see it here https://mydaytodo.com/core-data-migration-in-ios-app-swift/

Get updates?

If you find any of my posts useful and want to support me, you can buy me a coffee 🙂 https://www.buymeacoffee.com/bhumansoni Or you can  buying or even try one of my apps on the App Store.  https://mydaytodo.com/apps/ Also, if you can leave a review on the App Store or Google Play Store, that would help too.
Categories: iOSSwift

0 Comments

Leave a Reply

Avatar placeholder
Verified by MonsterInsights