Why before using code for copy db to document folder Xcode automatically copy db and it is empty? - ios

I used to suing code for copy db from bundle to document folder. But when run Xcode project before run my code database has copied to document folder and another problem is: Data base copied is empty. And when my code check document folder for exist db result is YES therefor not copy complete database to document folder.
I am using Xcode 8.1, iOS8.
Please help me.
Thank you.

Perhaps, you use XCode generated code, and on first call to managedObjectContext database is created. The simplest solution is:
check if database is empty (amount of records for some entity == 0)
if yes, copy your file from the bundle to the destination directory.
If your database is used in read-only mode you do not need to copy it into Documents folder at all - you can use it from bundle directory directly.
In this later case you'll need to set up proper key in persistent store options' directory and, copy index files, if you use SQLITE store into bundle as well.

Related

Default value in core data

I have core data file generated by my app. This file contains a data. But I don't want this file generate in every user device. I want put this file in app and every user uses this file. How can I do this?
I have find answer here:
https://gist.github.com/xavierchia/ef43abb270003ae63e5bbb7eb5404645
I need just copy my SQL files to project and copy them in app directory.

Load an SQLite Database preloaded into Core Data

I have a database preloaded in SQLite: "BenedictusCoreData.sqlite".
I add it to the project in XCode.
I modify the BenedictusCoreData.xcdatamodeld to create the entities and attributes with the same name and type as the tables and columns of my sqlite preoloaded.
I run the app in the simulator without writing any additional code. I print the main bundle path and go to the sandbox of the application. And inside /Library/Application Support/ I find the three files: .sqlite .sqlite-shm .sqlite-wal
I open the .sqlite and obviously it is empty, however Xcode has created the tables and the columns with capital letters, adding "Z" to the tablenames and "Z" to the column names, appart from adding new tables "Z_METADATA" "Z_MODELCACHE", "Z_PRIMARYKEY".
At this point, I assume that it is not a good idea just to copy my sqlite from the bundle into the "Application Support" folder and rewrite the file, because the structure is totally different.
So I am lost about how to proceed in order to copy a sqlite preloaded database into CoreData the first time you open the app.
May be you can help me.
In the appDelegate you can change the default .xcdatamodel name to the one you added. Then xcode will load your preloaded data .sqlite file instead of the default one. You should search it up there's tutorials on this

Do i need to Enable WAL mode to copy .SQLite from bundle to Document directory?

My current application only copying .sqlite file (WAL MODE = DELETE).
Everything went smoothly , the new copied myfile.sqlite also contain data perfectly.
When I execute a fetch request , the execution has no error , and BOOL (success) is 1 . BUT the returned array always EMPTY.
https://www.appcoda.com/core-data-preload-sqlite-database/
This tutorial told me I neeed to copy 3 files (.sqlite , .sqlite-wal , .sqlite-shm) in to Document directory .
Does this mean I need to Enable WAL mode and re-do everything from the start? In Xcode 10 we still cannot copy just 1 .sqlite file? Can someone clarify this?
Assuming that you mean journal mode, and not WAL MODE (and that you mean Xcode 8.x, since Xcode 10 doesn't exist yet), no you do not need to copy any additional files or change modes. With journal mode set to DELETE there's only the one file. You don't need to create extra files to make copying work.
But there are several other problems you might be having, which you should check on, and if necessary post additional questions about:
Maybe the copy phase isn't actually working as perfectly as you think.
Maybe you're not adding the SQLite file to the persistent store coordinator-- so it's not available when you do the fetch.
Maybe your fetch is misconfigured and is trying to fetch data that isn't present.
As for your question though, no you do not need to copy other files, or change the journal mode to force those other files to exist.

How do I get my pre-populated Default.realm file onto a device?

I have a realm file that is already populated with data that needs to be there when the app is loaded on a device.
What can I do to get the realm file onto my device for testing and what do I need to do to make sure it is already there when someone downloads the app from the app store?
I am using Swift.
Add your database file to the Xcode project, i.e. "preloaded.realm"
Make sure you select the add to targets, when first dropping in your file
Then (taking from the migration example) you can do something like this to copy that preloaded file to your default directory. This will create a read/write realm
// copy over old data files for migration
let defaultPath = RLMRealm.defaultRealmPath()
let defaultParentPath = defaultPath.stringByDeletingLastPathComponent
let v0Path = NSBundle.mainBundle().resourcePath!.stringByAppendingPathComponent("preloaded.realm")
NSFileManager.defaultManager().removeItemAtPath(defaultPath, error: nil)
NSFileManager.defaultManager().copyItemAtPath(v0Path, toPath: defaultPath, error: nil)
Here is a link to that general code https://github.com/realm/realm-cocoa/blob/master/examples/ios/swift-2.2/Migration/AppDelegate.swift
You'll first have to create the realm file you want to ship with your app. Once you have that, add it to your app's Xcode project and copy it into the bundle (which Xcode should do automatically).
At this point, the app should be able to access the bundled file (you can use NSBundle.mainBundle().pathForResource(_:ofType:) to get the path).
You can either create a read-only realm at this path (see RLMRealm(path:readOnly:error:)), or copy it to your Documents directory to create a read-write realm file.
You should refer to our migration example for more details on how to do this.

Sqlite file copy while launching Application

How to store database from one sqlite file (which is in our bundle) to sqlite file (which is created while we using core data) while we launching application first time.
Should we copy each entity, each row in loop or Is there any other way to do this.
You can put sqlite file directly in your bundle, for example you can create sqlite file with SQLite manager in firefox. Then when start your app you can check if file exist in the document directory, if not you need to create it. This only the first time.

Resources