Using CoreData along with a bundled SQLite database? - ios

I’m new to the iOS dev party so now I’m struggling with some issues. I want to create an app that uses a database for returning some information to the user. For this I added a SQLite database into the project and used this (https://github.com/stephencelis/SQLite.swift).
Now I want to add a new feature to the application that will let the user add some bookmarks in order to save some pieces of that information. For this, I need to update the database which is causing me some major concerns: how can I update the app without overwriting the user’s bookmarks? What happens if the user skipped two updates prior to the 3rd one? As you can see, this can turn into a huge pain.
So I was thinking on using this bundled SQLite database only as a readonly db and along with it to use CoreData for storing the user’s bookmarks. What do you think? Is this the right way to go?

Core data as you know is a persistant data that will be with your application. Well i still cant understand what seems to be the problem with maintaining the SQLite database but if this is a problem then i dont think core data will solve it. Having core data along with the SQLite database with you explicitly not chaning the DB and just "remembering" bookmarks in core data and fetching them from SQLite might do the trick. This is a conceptual problem that you are facing.

CoreData is a great framework, but it is not trivial and it add complexity that maybe is not required from your project.
If the bookmarks are not a lot of data you could store them in the NSUserDefaults.
this example can be useful :
(http://ios-blog.co.uk/tutorials/quick-tips/storing-data-with-nsuserdefaults/)

Related

How the Core Data Migration will work for two projects?

I'm working on an app which using core data (one app is already live on the AppStore). OK, now I want to make it fully functional for iOS8 and client also asked me to make a good change in UI and its structure so I started a complete new project and decided to code it my self. Everything works fine, then client emailed me and confirm that, new version of app should store & fetch existing values from the database if existing app will get update. I feel bad here.
I know core data migration is possible ( I read this too, but there's some glitches in my mind about this concept.
As I told, I started a new project though is it possible to add a new version of the model?
Our entities and attributes names are not same. I named it the way I want.
Our model name is same. e.g. Somename.xcdatamodel
My app will update to the existing app on the AppStore.
How I can migrate the existing core data database to the new one?
Is this possible? How?
Any suggestions and help on this would be appreciated.
Note:
I read this question, Core Data Migration: How to delete the Core Data stack? and found that there's no issue if I delete the previous model? What you suggest?
If you need to migrate and keep the existing data, you should read more about writing custom policy Core Data Versioning Guide. Read this guide completely to get to know which category your app migration falls in.
1. Lightweight migration.
2. Or you have to write your own custom policy.
As far as i can tell from the details you provided it looks like it might be a lightweight migration for which you do not have to do anything you can just add a new version to xcdatamodel and mapping models. If not then you have to write your own custom policy and mapping models.
Also make sure you test upgrade properly.

Modifying the Core Data Model Requires New Version Every Time?

I've been searching around and let me get this straight, every time I change the attributes of my entities, I need to create an entirely new version of my model? I understand that the SQLite database is now out of sync with my data model, but can't I just delete the sql files and re-run my simulator? This is what I've been doing, but it doesn't seem to be working.
You are correct, any changes to the core data model must be made in a new core data model version. You can avoid having to do this for development builds by resetting content and settings in the simulator, or deleting the old app version on a device before installing. For store builds though, you must correctly manage your model versions so that user's will have their persistent store properly migrated to a new store with the new model. Versioning is required so that lightweight migrations can be performed, without you having to write custom migration code. If you don't want to version your model, you are bound to writing code to properly migrate your entities. This can be a lot of work and difficult to maintain, you'd rather use lightweight migration and versioning.

Is it possible to add a new attribute to Core Data without having issues with users and their current data?

I want to add a new attribute to my Core Data model but I'm worried if I do the users data will no longer work, and they'll have to redownload the app. I've had issues before where I change it a bit (though through deletion) and it causes big issues with the app where I need to reinstall it.
How should I go about doing this?
The process is called Core Data Migration: Ray Wenderlich has a great tutorial on it here. If you are just adding a new attribute, it should be (relatively) simple to do an automatic/lightweight migration.

Core Data migrations with Magical Record

I use Core Data and MagicalRecord for my application's database.
How can I migrate my database without uninstalling the application when I add a new model to the database?
Take a look at this question, and the tutorial posted.
What is common data migration strategy for Core Data in iOS development?
Here's another:
http://www.raywenderlich.com/27657/how-to-perform-a-lightweight-core-data-migration
Have a look at this How to migrate core data by deleting old one on App Update
Since you have already done the pre configuration for Magical Record, everything else will be taken care of, Cheers!

Making Changes to Data Model with SQLite iOS

I'm reading data from a pre-populated sqlite database using CoreData. Everything is fine and dandy, except when I decide that I want to add another entity to the CoreData model (and another table to the sqlite database). I've tried several things, including making the changes to the model in Xcode, removing the sqlite database, and then hoping it generates a new database like it has in the past.
However no matter what I've tried, I get this error:
The model used to open the store is incompatible with the one used to
create the store.
Maybe I'm not doing this the right way, but it seems like Xcode makes it difficult to change the data model, which doesn't make sense to me when you're developing an app.
In general, if you delete your app and recreate it, the boilerplate code that comes with the project will generate you a new (and like you say, compatible) database.
Also, CoreData is intended to be used as an object graph with persistence; what it does with the generated SQLite DB as a result of providing this is its own business.
If what you're saying is: 'I have a valid CoreData store file and model, and I'm trying to hand-upgrade the store file to match a new model', you're breaking the rules of the framework. To morph an incompatible store to a new model, you need to use versioning and mapping models (without directly touching the SQLite DB).

Resources