Reusing core data model when updating app - from scratch - ios

My question is fairly simple though I couslnt find anything specifically answering it till yet.
I have an obj-c app that I'm updating fairly often. I would like to create a new swift app with the same bundle id to replace the older one as an update. I have like 8 core data model versions within my old app that I would love to migrate to the new swift-from-scratch-app, so my users wont lose their data. Is it even possible??
Thank u

If you're using the exact same bundle ID (this is key), then from the perspective of iOS, it is considered exactly the same app. Consider your users having version 1.x of your app installed on their devices and you release 2.x using your new Swift project. Your user, nor iOS, will be able to tell this is a brand new app written in Swift.
The gotcha here is that unless you use the exact same model names, CoreData won't be able to be initialized with existing data and you'll be forced to write a custom migration for this.
My suggestion to you is to copy the xcdatamodel file from your old project and change the language of the code it generates once copied into the new project.
Then regenerate your entities in Swift and continue working like nothing ever happened.

Related

App update (complete redesign) maintaining existing core data info... possible?

I have an old app on the app store that needs to be updated to work with iOS11 and the notch. It's an older project so there's a LOT of problems and it would be much easier to just rebuild it (it's a simple app). The original app was built with Swift2 and Xcode7 I believe.
If I recreate the core data "structure" exactly as it was in the old app and then upload the new app as an "update"... is there any possibility it will migrate the core data from the old app to the new one? As I understand it, unless I make changes to the structure of the core data itself (add new entities, etc) it should just migrate automatically, but it's the "whole new app" that has me confused here.
Thanks for any help/suggestions.

Replace core data model in new version of app without having previous source code

Hi I struck at core data migration issue. I have developed a app which is new version of an existing app in Appstore. But i don't have source code of previous version and don't know exactly that whether core data was used in old version or not. So how to update my app (which has core data) to Appstore without any crashes. Any quick solution please?
Since you mention in a comment that you want to ignore the old data, you don't need to do any kind of migration. Model migration is about updating existing data to work with a newer data model. The only danger would be if the old app used Core Data and your new app attempts to use a persistent store file with the same file name. Then your app would attempt to load the old data, since it would find that file with the correct name.
The easiest way to test this is:
Install the existing app (from the app store)
Use this app until you're sure it's saving some data
Install the new version from Xcode. Since it's an upgrade, it will overwrite the app store copy.
By then you'll know exactly what happens when upgrading. If you get a Core Data related crash, change your Core Data setup to use a different filename. Then delete the app from your iOS device and repeat the test from above.

Start with core data in project?

I will start developing a new project for iOS. I am unsure for now if I should use core data in this project, or not. I will find out along the way, while developing (most likely with some coaching help).
Now my noob question is: Should I, while creating my new project, already check the option 'use core data'? Or shouldnt I?
Basically my question is: What is more difficult, removing core data when I checked the option and I will not been using it after all? Or adding core data when I did not check the option and I will be using it after all?
I have found it is not super difficult to add after:Implement CoreData into an existing project using Swift
But still, it seems like alot of hassle, so I now ask u!
Thanks for the answer in advance
It doesn't matter. Starting with Core Data just prepares the core data code in the app delegate and makes an empty model file, both of which are
1) easy to create yourself, or
2) copy from a new empty project started with core data later when you need it.
And if you already have it in your project, but you don't use it, it is also no problem. I found that almost all projects need core data after a while, so I usually just let it create it from the start.
Xcode just allows you to check using coredata to prepare already a coredata for you project.
BUt it is very simple to add or remove it later.JUst simple as File>create or File delete.
In a words don't worry about creating in the begining or later.

How to fix crash when modifying Core Data with a new version of an app in iOS

I am struggling with an issue related to Core Data.
In my app, I use Core Data for storing and retrieving the values. In my latest version, I added some attributes to entities from the old version. I updated the app from the App Store, and when I click on the page where we need to display the contents from Core Data, it is crashing.
If the old version of the app is removed and the new one is downloaded, then it doesn't crash.
My requirement is any alternative there is to fix without having to remove the old version of the app (merging the old version with the new version).
Any suggestions on how to fix this issue?
Thanks in advance.
Did you setup versioning of core data? IF so, in your Xcode project navigator, your .xcdatamodeld should be drop down with multiple .xcdatamodeld underneath it. And then in attribute inspector with the parent .xcdatamodeld selected you pick your current version.
If the changes are minor, this will take care of it's self when you load new app version ontop of older version.
If the changes are major, you need to setup handling for this in your app delegate to tell the app how to move data between the two versions.
You have to migrate the Core Data store from the old Version to the new one.
This tutorial show's how this works: http://www.raywenderlich.com/27657/how-to-perform-a-lightweight-core-data-migration

Using one xcdatamodelId in two app

I created one project with core data that will work with unchangeable database. And I don't want to write code in this project , that will programmatically populate this database. So, I create second project with core data, add existing xcdatamodel from first project without copying(only references). There i populate my database, open it with mozilla plug-in and it successfully filled. Then I copy ,my *.sqlite file and manually replace it with old file in first project. It causes error:"The model used to open the store is incompatible with the one used to create the store". But I use for both files the same xcdatamodelid. Where my error?
Sorry for my english, I really need help.
P.S. when I open sqlite file from first project and second (with commented code of populate base) in FileMerge - second is already empty. I appreciate any advice or help.
Karoly S nicely answers the question. I have a hint that I frequently employ that may prevent this out of sync situation. Instead of two Xcode projects trying to share one model file, just create one Xcode project with two targets. Each target will use the same model file, any class definitions derived from that model, and possibly other code. My second target is a Mac OS command line program that generates the database, while my first target continues to be the iOS app that reads that database. The Mac OS target will overwrite the database file in a project subdirectory, ensuring it's up-to-date. If I make any changes to the model, Xcode knows to update both targets.
Did you change your Model Definitions in any way? The error you are seeing is because there is some difference between the model from when you created it, and again from when you are trying to reference it. Are you running on the simulator? Try to delete both of your apps to clear the data related to it, as it might be out of date. Afterwards simply rebuild both of your projects as that will update your core data database.
EDIT: To clarify a bit more, your core data model is out of sync, this is generally caused by you building and running an app, and a database being created, then redefining your object model, this can be done in a variety of ways, most likely caused by the addition of an attribute or entity. So when you are trying to load the database there are fields that the app and core data are looking for, but are not there because they did not exist when the database was created. I hope this helps.

Resources