Sqlite to Core Data Migration

SQLite to Core Data Migration

I think @SaintThread hit the main issue here. The reason for it is that Core Data is not a wrapper around SQLite-- it's a different API with different assumptions that just happens to use SQLite internally. Core Data doesn't attempt to be compatible with how you'd use SQLite if you were using SQLite on its own.

That said, if you still want to migrate, you'll have to design a Core Data model and then write fully custom code to migrate from your existing SQLite file to Core Data. Your code would need to read everything from SQLite, convert it to the new Core Data representation, save the changes, and then remove the existing SQLite files.

When removing the existing SQLite file, make sure to also remove the SQLite journal files (if any).

Migrating user data stored in sqlite to core data in app upgrade

You can use core data. Create your core data model and stack, and your migration process consists of querying chucks of data from your sqlite database to insert them into the core data database.

Let's say that you have three tables in your sqlite. Person, Vehicle, and Property. You will create the equivalent entities in your core data mode, then you will have to query information from these tables to insert them into core data. You need to get familiar on how to insert data into a core data database, and how to use it on a multithreading environment (to avoid locking the application during the migration process).

Here's a few things to keep in mind:

  • Try to save in chunks. Do not load everything into memory, but also you must avoid making numerous save calls into your NSManagedObjectContext instance. Every save call into the context is IO. Balance IO and memory depending on your data usage.
  • Index the properties you have on your existing entities that can be indexed. This will help you build relationships (if any)
  • Ask the system for time before starting the migration. Use UIApplication's beginBackgroundTaskWithExpirationHandler: in order to be sure to have enough time from the system to continue the migration (in case the user sends the application to the background).
  • Do not bother cleaning the sqlite database. Delete the database file when you are done with it.

These are some of the things I can advice to you for now. If i think of more, I will edit this post. Best of luck.

Migrate from sqlite3 to CoreData?

I think migrate from SQLite to Core data is not a Good idea, Also migration consume lots of time. So my suggestion is Please try to understand flow of exiting app.

SQLite and Core data have his Own advantage, You can shown below

SQLite:

SQLite is the most used database engine in the world and its open source as well. It implements a transactional SQL database engine with no configuration and no server required. SQLite is accessible on Mac OS-X, iOS, Android, Linux, and Windows.

It delivers a simple and user-friendly programming interface as it is written in ANSI-C. SQLite is also very small and light and the complete database can be stored in one cross-platform disk file.

The reasons for the great popularity of SQLite are its:

Independence from a server
Zero-configuration
Safe access from multiple processes and threads
Stores data in tables with one or more columns that contain a specific type of data.

Core Data

Core Data is the second main iOS storage technology available to app developers. Depending on the type of data and the amount of data you need to manage and store, both SQLite and Core Data have their pros and cons. Core Data focuses more on objects than the traditional table database methods. With Core Data, you are actually storing contents of an object which is represented by a class in Objective-C.

Although they are fundamentally different, Core data:

Uses more memory than `SQLite`
Uses more storage space than `SQLite`
Faster in fetching records than `SQLite`.

Why does my sqlite database not get migrated when versioning Core Data model?

Core Data migration does not migrate the data to a new file, it updates the persistent store file in place. Your steps are correct except for #4-- because at that step you're telling Core Data to use a different persistent store file than the one you've been using. That's why the data is missing, because you're opening a different, new file. You're specifically telling Core Data to not use the existing file with previous data. If you want to update the persistent store to use the new model version, you need to use the same file name so that the existing data can be found and updated.

For what it's worth, there's no reason for the persistent store file name to match the model name. They can be the same, but if you include version numbers in the persistent store name then things can get confusing. Changing the persistent store name isn't normally part of model migration.

You can move data to a new file if you want, but this is not normally part of updating the data model.

SQLite database migration to Core Data with updating live version on App Store

Your approach is correct. In the new app version, check if there is an up-do-date Core Data persistent store, if not, read the old data, copy it to Core Data, delete the old sqlite file.

You can leave that code in there for a long time - it just won't run if there already is a valid Core Data installation.

Migrating from Core data to sqlite in new update of app

If your actions will correct and grammar, users willn't lose any data.
How I understand, in your next version of app you will managing data by any sqlite framework, then you just need .sqlite data file. To get it .sqlite file path you can:

NSString *dataFilePath = [[[self applicationDocumentsDirectory] path]
stringByAppendingPathComponent: @"YourAppName.sqlite"];
NSURL *dataFileUrl = [NSURL fileURLWithPath:storePath];

When applicationDocumentsDirectory method implement like this:

- (NSURL *)applicationDocumentsDirectory
{
return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}

If I understand something incorrect, write it in comments.



Related Topics



Leave a reply



Submit