Create model classes for Coredata database in iPhone - iphone

I am creating a static library, and the database for the project is created programatically. It doesnot use any xcdatamodel file. I am using core data and I want to generate the model classes for the database. Someone please help me out.

The .xcdatamodel bundle generated by Xcode is an undocumented format, and I know of no way of generating this other than through Xcode's model layout tools. I would not waste your time on reverse engineering this and just create the appropriate model you need in Xcode.
Your Core Data model structure should remain fairly static, so you should just need to do this once. Your application or library can then create and add data to the model programmatically after this point.
When it comes to generation of code that corresponds with your Core Data model, you can have Xcode generate this for you by opening the model, selecting an entity, choosing the File | New File... menu option, and then selecting the Managed Object Class file type. Pick the entities to create source files for you.
Additionally, you might want to look at 'Wolf' Rentzsch's mogenerator project for auto-generating these source files.

Actually Core Data is simply an SDK that uses SQLite3 in the background. So if you want to dynamically create tables then I would just use SQLite3 directly and not use Core Data.

Related

iPhone | Core data migration for a new version built from scratch

I am working on an App update and this version is completely built from scratch and has completely new coreData model schema. I need to write coreData migration from the older model. I have been looking at resources to write coreData migration but all of them need old model file. Since this version is completely new, I don't have the old model schema.
Is there any way I can write the migration such that it dumps the old model when user updates the app?
Thanks in advance
If you have the old App, then you can recreate the model from the compiled model file which is contained in the App bundle:
Sync the App to iTunes, if necessary.
Ctrl-Click on the App in iTunes, and use "Show in Finder" to locate the "YourApp.ipa" file.
Copy "YourApp.ipa" to a temporary directory, and use "unzip YourApp.ipa" on the command line to extract the archive.
In the extracted "Payload/YourApp.app" folder there should be a "XXX.momd" folder containing a file "XXX.mom" (XXX is the name of the model). This is the compiled model file.
In your Xcode project (or a temporary project), create a new "Data Model" file "XXX.xcdatamodeld".
Select this file and choose "Editor -> Import ..." from the menu.
In the import dialog, choose the "XXX.mom" file from above.
Without the old model schema, you cannot do a migration. Well, that's sort of logical.
You only perceivable option is to reverse engineer the old model schema and take it from there.
Then you construct your new schema by adding / deleting / changing entities, attributes and relationships.
If you can extract the data from the existing store, just write a custom export function and insert it into your brand new core data store.
You can do migration by reading old database with pure sqlite SELECTs and inserting data into new database through coredata model. I've made this in my software to import data from concurrent software :)

generate Xcdatamodel file from JSON

Is it possible to generate xcdatamodel file from json? Here's how i imagine it would work:
Given a JSON file, going through some program with some custom logic (will probably be written by myself), will generate xcdatamodel file for me to use in XCode project. My question is: is it possible? has anyone ever done it?
I don't think it's possible to create an xcdatamodel file in code. The format isn't documented anywhere and includes layout properties that are only relevant in Xcode's GUI model builder. Those files are also converted to "compiled" .mom files during the build process.
However, you can create an NSManagedObjectModel entirely in code. See this listing for an example: Creating a managed object model in code (Core Data Programming Guide).
NSManagedObjectModel also implements NSCoding, so it should be possible to persist the model you generate to a file using NSKeyedArchiver.

Is there a way to populate data via xcode?

I want to input data into a sqlite database so that core data can read from the file but I was wandering if there's a way of populating the sqlite data via xcode or do I have to spend lots of money on an sql manager of some kind like RazorSQL to do it?
I suppose you could do the following:
Create a CoreData-based project that uses the same data-model that you're going to use for your project. To be clear, make it a Mac project and not an iPhone project. Make sure that the project is using an SQLite store, and not something like XML or binary.
In Interface Builder, add a built-in CoreData Entity editor to your view and configure it to hook up with your model (most of this should be automatic!). Then, build and run this application, and you should be able to add as much data as you like. When you are done, simply find out where the persistent store was kept (usually somewhere like ~/Library/Application Support/MyAppName/) and copy that file for later use.
Then, you should be able to use that .sqlite file in any app that is made to work with the same data model. Good luck, and let me know how it goes!

Prefilling Core Data for a read-only application

i am working on an application that displays read-only data i am shipping.
it is more of a book.
It is easy with SQLite but i am not satisfied with the performance and trying to use Core Data.
The issue is with pre-filling Core Data is that it is a hard process.
My question is:
Is it possible to build an assistant iphone application (for me to use) which uses the same data model for pre-filling.
and then take the populated .xcdatamodel file and use it in my original application?
I hope this makes sense :)
Adham
I believe what you're asking is whether you can create a CoreData database upfront and copy it to the iPhone. Is that correct?
This article will help. Here's a quote:
I thus suggest the following five-step process:
Create your data in a comma-separated file, typically placing each row of data (an entity) in a row of the file and separating different columns (its attributes) by commas.
Write a standalone program and copy in your .xcdatamodel file from your main project.
Write code in your new program that parses your comma-separated file and inserts the information into a Core Data persistent store that should be identical to the persistent store in your main project.
Run the program in the Simulator
Copy your data from the Simulator's documents directory into your actual project's bundle.
It's possible, I've done it. I made a desktop application to read from a CSV file using the code here:
http://www.mac-developer-network.com/columns/coredata/may2009/
I just had to alter the way the CSV part worked, and change the model.
I copied and pasted my model from the model builder into the iPhone model. (Clicked on the "grid" area, selected all, copied)
Then I took the sqlite database the desktop app produced (found it in Application Support, in the folder for this application) and put it into the resources folder
I made some code to copy the sqlite into the documents folder on the iPhone (if it wasn't already there) at startup, in the applicationDidLaunch method. It's possible that having it in the resources folder is no good. Even though you're using the database as read only, Core Data may want to write something to it. Not sure about this though..
I used the sqlite file in the documents folder in my Core Data set up.
The desktop and iPhone Core Data sqlite file seem to be exactly the same format. You can transfer one sqlite file to another application (iPhone to iPhone too) as long as they have the same data model. In another application, I used NSXMLParser to create the Core Data sqlite file, then transferred it to another app, both on iPhone using the Simulator.
Yeah, your data source can be whatever you want it to be. The other suggestions are good ones. Create a managed object model (.xcdatamodel) identical to what you want to use in your app. Read in the data from your file, create a new instance of your managed object and populate it from the file. Then save, and dive into the bundle in the iPhone Simulator and copy it over. This has the added bonus of being in exactly the format you need, with all the helpful metadata. Copy your object model and your managed object classes and you're good to go.
Note, though, if you really intend for it to be read-only, and you're using it at install, it will be installed in your finished app's bundle (under Applications/{SIGNATURE}/Myapp.app). If you intend to edit this database or allow a user to save to it, it's a better idea to copy it to the Applications/{SIGNATURE}/Documents directory where your user database lives.

Use Core Data For Storage for an existing Iphone project?

According to the Apple Core Data Tutorial, in order to use Core Data you have to:
In the Options section, select the switch to use Core Data for storage.
How do it for an existing project? Do you have to import some files? Can that setting be activated later on?
Thanks
Selecting that option automatically adds the CoreData.framework, an empty data model, and a little bit of Core Data code to the AppDelegate and View Controllers classes.
If you want to start using CoreData in your existing project;
1) Add CoreData.framework to the project.
2) Do "New File", go to "Resource", and add a new "Data Model" to the project.
3) Create a temporary new project (check that use Core Data box). Then, you can look and see the Core Data-related code that it added to those template files. Carefully merge that stuff into your existing AppDelegate.
4) You should be all set to pick up the tutorial from that point.