I am pretty new to using Core Data, so I'd like to ask how I graphically add items to the database I am making. With graphically I mean like in navicat I can edit things with editor.
Core Data is a object oriented framework that provides object persistence, not a visual database editor. Xcode contains a visual tool for creating and editing Core Data models, which are similar to database schemas, but there's no visual facility for managing or manipulating the data that a model describes. It can take a little while to understand Core Data; I'd suggest starting with Apple's Core Data Tutorial for iOS.
May be am not clear with the term graphically. When you start creating your model using Core data, you need to choose a data model from project resources (of type .xcdatamodel) and when you select this,it wil open a model editor for you. you can create as many entities, related attributes and establish relationships. Hope this will help you.
~Manoj.
If you just need to instantiate a new entity and add it to your data store, you'll use the method insertNewObjectForEntityForName on NSEntityDescription to create a new instance of your entity and insert it into your context. You have to call saveChanges on the context to persist that new entity to your data store. Core Data is a really powerful framework, but it isn't something most developers can just start using blindly. Read the Core Data guide, download some of the sample code, and you'll be rocking in no time.
Related
I have an app that already uses Core Data and I now would like to add a new Entity with its own attributes to Core Data. When doing this, I will not be touching any other Entities.
I would assume that this would require Core Data Migration; however, upon adding this Entity, my app still appears to work correctly with no Core Data issues.
I am happy to proceed with migrating my data, I just do not want to waste my time doing so if it is not required.
It doesn't need manual migration.
Add a new version for the data model.
Add the new entity to the new
model.
Select the new data model as the current model.
I have done the following to try and answer this question:
I have scanned the Apple documentation on Core Data
I have done this very good tutorial: Core Data from Scratch
I have scanned other articles in an attempt to answer this question
Further detail on my question:
I have an existing iOS application. To achieve persistence, I am currently, by hand, marshaling and unmarshalling JSON files which I store in a directory on the phone. I would like to instead use Core Data in the same kind of way that you might use Hibernate or another ORM tool with my existing domain model.
Which is to say I would like to have something like:
MyDomainObjectDAO
save( myDomainObject )
load( id )
etc. etc.
Where the implementation of those methods involves handing an instance of myDomainObject to something like an ORM context which then stores the object. C'est possible?
I imagine that I could copy all the values from my existing object structure into managed objects created by the core data Apple tools, but I wanted to ask if there was a better way to do this. I would like to keep my convenience methods on my domain objects
If I understand your question correctly you are looking for RestKit. What you will do is create a relationship between your object model and your JSON. You will find that you can almost do it all on the fly without creating any NSObjects manually or parsing any JSON. All you have to do is create the CoreData scheme and create some mapping instances. RestKit will convert your incoming JSON to these objects before persisting them into CoreDate. RestKit is awesome though a bit slow!
I am using CoreData in my iOS application. I face a problem most of the times while creating NSManagedObject classes.
This is what I do:
I create an Entity in .xcdatamodeld file.
Create attributes and relationships.
Choose option Editor->Create NSManagedObject Class to create .h and .m classes.
In .h and .m classes, I create some of my custom methods for fetching/saving objects.
So far so good. But afterwards in future if I have to change some attributes, I repeat step 2 and 3. But this time all of my custom code written in step 4 are removed automatically.
So my question is how can I update the existing classes? Instead of using option Editor->Create NSManagedObject Class which removes all my custom code.
Any help is appreciated.
Update:
Tested both approaches (Categories and Mogernator) and looks fine to me. But I have choosen Categories of being a pure Xcode approach. I don't want to take the risk of any 3rd party which may break in future due to XCode updates or can cause problem of data migration.
Thanks to #Tom Harrington, and #Valentin Shamardin for guiding me :)
To make some additional methods or other stuff for your Core Data model classes you have to create Categories. This approach is used by Paul Hegarty in Core Data lections.
The best way to handle this is to use mogenerator to generate your model classes instead of having Xcode do it. With mogenerator you get two classes for each entity:
One that is re-generated every time you rebuild the model classes
One that is a subclass of the other, which is only generated the first time you build model classes and which mogenerator never changes afterward.
As a result you can put all your custom code in the subclass, and no matter how many times you re-generate your model classes, your code is never overwritten.
Whenever yo change or update your core data object model it becomes incompatible and cannot be open and as a result it gives crashes.
For this you need to perform Core Data Model Versioning and Data Migration.
Versioning is nothing just only provides you which model version is application going to use. Changes and update in core data model like modifying any attribute of entity or adding new entity. It is related with Core database model. Consider your application going to use 1.0 ver and using database model of 1. if u have some changes in database model its version increases and now you application going to use next version ie 2 of core database model
The details refer to this Apple Doc for Core Data Versioning
Also this will result in old Data loss. For this you need to perform Migration Process.
During migration, Core Data creates two stacks, one for the source store and one for the destination store. Core Data then fetches objects from the source stack and inserts the appropriate corresponding objects into the destination stack".
Please refer to Migration Process details on Migration on Raywenderlich
You need to use NSMigrationManager class and
I am developing an iOS app and have several object models. Now I want to convert them to core data managed objects, so creating the entities and attributes from the object instead of the standard opposite way of generating the objects from the model. What is the best way to accomplish this?
I tried extending from NSManagedObject instead of NSObject, then manually creating the entities and attributes and setting up the Class name for the entity but that didn't work :-(
Thanks
I recently did this for a big project with a very large and complex data model. Here is a workflow that I found to be very feasible.
First, I create a new data model in Xcode and populate it by hand. This is a very good exercise as it forces you to review your model and perhaps take advantage of simplification opportunities.
Then, generate your class files with Xcode from the model. If you have any special code that should be included in the objects, add them as categories in separate files (e.g. Event.h & Event.m generated by Xcode, Event+Additions.h & Event+Additions.m with your code).
I have an iOS app on App Store that utilized Data Core with SQLite engine. The updated version that I plan to update needed to add several more attributes (fields) to an entity (table). What is the simplest way to handle this without losing data on already existing SQLite database?
The way to do this is with what's called a "Core Data Migration".
Here's the Core Data Model Versioning and Data Migration Programming Guide
Also, some initial posts:
Data migration with core data
core data migration
http://www.timisted.net/blog/archive/core-data-migration/