Pre loaded database on iPhone? - objective-c

I developing an app that is read-only. The data has relationships, so I cannot just use a plist or something similar.
Questions:
Should I use Core Data for such a requirement?
If so, how would I enter the data and then release the app with that data?
How would I make it so that the app doesn't need to re-populate a DB every time it loads?
Is there a way to create a Core Data model using sql commands with sqlite (i.e. insert into, etc)?

You may use an SQLite database to accomplish this.
Create the model in your iOS app.
Create and populate the database in a Mac OSX utility command-line app
Copy the sqlite file into your iOS app and link it with some code
Work through these two tutorials, line by line, and afterward you will have a good enough understanding (and code sample) to complete this task in your own app.
Core Data on iOS 5 Tutorial: Getting Started
Core Data on iOS 5 Tutorial: How To Preload and Import Existing Data

In my short experience with the iPhone, you have two options.
Write a data import function and run it on the first application launch.
Use solution 1, but build the initial sqllite file in the simulator, and then on first application launch, copy it into the app's documents directory.
From past experience, option 2 is much quicker for user experience, and the preferred solution.

You can write a utility project that imports the app's data model and use throwaway code n that project to populate the Core Data DB. Once the DB is populated, simply copy the actual file to the app project's resources folder and then when you set up your persistent store, use NSBundle to return the path to the DB file within the built app.
And you're done.

Related

Ideal location of db on IOS

I am using phonegap for IOS app development. Now, phonegap tries to find the db by default in 'NSCachesDirectory', but whenever IOS runs into memory problems, it tries to delete data from 'NSCachesDirectory', so, the data is not secured. If i am not mistaken, this problem was solved in cordova 2.1.0, where the back-up of data is taken and then restored afterwards. So, just wanted to confirm if i am heading in the right direction or data itself can be stored in 'NSDocumentDirectory' so that data is secured and somehow phonegap looks for db in 'NSDocumentDirectory' and not Caches direcory. Thanks.
Only documents and other data that is user-generated, or that cannot otherwise be recreated by your application, should be stored in the /Documents directory(You can store DB in Documents directory)
Data that can be downloaded again or regenerated should be stored in the /Library/Caches directory.

core data database values life cycle

Am developing an application using core data. Every time I delete the application from the emulator all the data that was inserted using core data is deleted. does this mean when i publish my application my data base will be empty? I want the data base to have values when i publish it or install it to anther device.
You can create a database and package it inside your application bundle for distribution. Then, whenever your application starts up, before using any data it can check to see if there's a copy of the database in the working directory. If not, it should copy the packaged one.

Objective C: How to store static data to Core Data when application launches

I am intending to use Core Data to store a static list of data which consist of objects with attributes (e.g. object = person, attributes = weight, height etc).
Any advise on what is the best way to approach this? What raw file format should I need to use and what is the best way to load the data from the raw file to core data?
Appreciate any advise and help on this
If the data set is large, you should include a prepopulated SQLite store in the app bundle.
Create a separate project in Xcode and import the data model and NSManagedObject subclass files (if any.) Write code to import or create the static data and write it all to the persistent store.
Copy/add the persistent store file to the release project. In the release project, set the store pathway to the location of the store file in the app bundle using [NSBundle pathForResourse:Type]. In the persistent store options, mark the store as readonly.
That will let you set up a prepopulated, readonly Core Data stack that will efficiently handle thousands of objects for you.
I'd suggest shipping binary .plist file with initial data and on first launch dump it to CoreData.
Also may I suggest you not use CoreData if your list is going to be immutable. Just read the plist on app startup and keep it in some AppDelegate's instance var.
Update (to reflect comment):
If it's a few thousand records - you SHOULD import it to core data as it would improve the performance of data retreval. Also - I would not suggest keeping ALL of them in the memory.
To get data into plist you could use some other programming language to select all the data from database and export it to xml plist (there are libraries for virtually any programming language). Then by using property list editor you would be able to export it to binary plist.
Then depending on what you need to do with your data - you can import it either in background or just show a progress indicator/bar to the user while using main thread for import. I believe plist would take few MB's. Also - try benchmarking a thousand records just to get approximate estimate of how long data import would take.
1, You could fill in core data on startup from external source. On startup, read data from external source(SQLite database or XML file), then insert the data into core data;
2, Provide pre-filled in SQLite database. For this we’ll let Core Data create the database structure for us based on the model, and then we populate the database with a utility app. The utility app could be a Mac or iPhone app that uses Core Data to populate the database via Core Data APIs, or some kind of program that fills in the SQLite database directly. Once the database is populated, just include it with the app and make the app use it as the default database if no database already exists.
Hope this tutorial will help.

Can I let Core Data use an already created sql database

I have a mosques database that has 1000 items ..
I want to use Core Data approach in accessing my database ..
I have already tried the SQLite approach to create the database
where I have a text Pad file with all data seperated by tabs
and then Import the data from txt file to sql file ..
Now this works fine ..
I want to know how can I import data from my SQL file to the newly created Core Data Project
Shall I add SQL file to resources ??
Copy it or not ??
I have looked at CoreDataBooks example but I think I'm missing something
I want to know the exact way for adding an SQL file to the resources of a Core Data Project ..
You can't.
You should regard the fact that Core Data uses SQLite as the format to save the file as an implementation detail, not to be used directly unless you really, really, really need to do that. For example, you can't expect Core Data to work alright if you also directly writes on to the SQLite file.
Instead, read the Core Data documentation, and import the data directly from the tab-separated text file to the Core Data context, and let the Core Data save it to the file. Yes it does use SQLite behind the scenes, but it's better for you to forget that fact.
Yuji and Dave DeLong are right on both accounts, however I feel like I should add that just because you can't realistically feed CoreData a pre-populated SQLite file doesn't mean you can't bootstrap your CoreData store from a SQLite file (or a text file or anything else.) It just means that you have to do the work yourself.
For instance, you could include your pre-populated SQLite file (with it's own, non-CoreData schema, etc) as a resource in the project. Then when your app starts up, if it sees that the CoreData store is empty, you can use the SQLite API directly to open/query your bootstrapping database and translate the results into operations that generate the desired object graph in CoreData. The next time the app starts up, the CoreData object graph will be populated, and you won't have to do it again.
The take away here is that while it's not "free," it's not "impossible." Many, many apps include built-in CoreData repositories that contain data. That data had to be bootstrapped from somewhere, right?

Testing on blackberry device - adding and removing app multiple times

It would be useful for many people to know how to completely remove an application from your device when testing.
I have downloaded my app many times now, and likewise have deleted it many times. The problem is when deleting the app, it does not remove things like the persistent object related to my app, or the images downloaded through the app. So, when I download the next build, I have no idea if something broke that is related to building the persistent object or fetching the images since those elements already exist from the last build.
I don't know if this is a cache thing. I don't know if this is expected and I have to use some utility to wipe this data after deleting the app. I can't really find much info through basic web searches.
Any information would be appreciated.
Blackberry Bold 9000. 4.6 OS. tested with both SD card and no SD card.
Objects stored in the PersistentStore are automatically deleted on uninstall if their interfaces were defined in your project. If they are from the standard BlackBerry API then they will stick around until they're deleted. E.G if you save a String in the PersistentStore it will stay in the PersistentStore but if you save a class you created it will be deleted on an uninstall. So if you want to have those objects be deleted automatically just create a wrapper class and save that.
Images stored on the filesystem will not be deleted until you or some application deletes them. However, it should be easy for you to write an app that clears everything out.
Another solution you could implement is making your app somewhat self-aware of its data.
Create a simple String value that you persist (or optionally, persist it in a Hashtable so you can store many properties this way) that includes "Version".
At startup of the GUI app, compare the stored "Version" against the application's current version. If the stored version doesn't exist, or if it exists and matches, take no action.
If it exists and does not match, automatically clean up old persisted data; or alternatively prompt the user to see if they want that data to be deleted (which one is better will depend on your implementation)
You can also use CodeModuleListener to listen for an uninstall event -- when that happens, you can clean up at that time as well or instead.
(As an aside and a bit of shameless self promotion, I am actually currently working on a shareable library for Blackberry that makes managing persistence much easier, as well as desktop data backup/restore. I'm doing this as part of the BBSSH project, but I'll be splitting it off into a separate library of core components and publishing it under a dual GPL/optional commercial license. It will contain hooks for data cleanup and data versioning. )