Hi I am working on core data for the first time. I have just created an entity and attributes for that entity. I want to add some data inside the entity(u can say i want to add data in a table), earlier i when i was using sqlite, i would add data using terminal. But here in core data i am not able to find a place where i can manually add data. I just want to add data in entity and display it in a UITableView. I have gone through the the documentation of core data but it does not explain how to add data manually although it explains how i can add it programmiticaly but i dont need to do it programically. I want to do it manually. Thanks in advance
You add data to a entity without an associated custom NSManagedObject subclass as follows:
NSManagedObject *mo = [NSEntityDescription insertNewObjectForEntityForName:@"MyEntityName"
inManagedObjectContext:aManagedObjectContext];
[mo setValue:aValue forKey:@"aKeyName"];
id aValue=[mo valueForKey:@"aKeyName"];
Core Data is not a table database. It is an object graph management system. As such, you deal with data within Core Data by changing the attributes of objects.
In the above example, I am changing the value held by the mo
instance which is a generic NSManagedObject. Because mo
is a generic NSManagedObject I use the setValue:forKey
to store the value within NSManagedObject's associative storage. The key names are set by the entities you create in the data modeler.
More commonly, you would create a dedicated NSManagedObject subclass whose attributes are the attributes and relationships of the entity. In that case the code above would look like:
MyManagedObjectSubclass *myMO = [NSEntityDescription insertNewObjectForEntityForName:@"MyEntity"
inManagedObjectContext:aManagedObjectContext];
myMo.attributeName=aValue;
id anotherValue=myMo.attributeName;
Trying to think of Core Data in SQL terms will only lead to grief. Core Data does not work like SQL. It works like a highly interconnected set of custom objects. You need to think in objects for Core Data, not tables.
I don't know why i can't comment to your original answer, maybe I don't have enough rep points? Anyways, to answer your comment of:no i want to add data manually, similar to what we do in sqlite through terminal or in sql server using sql query
. Download FireFox and search for an Add-On tool named SQLITE MANAGER
. It will allow you to open any SQLITE database, even those created by your app. SQLITE MANAGER
is a GUI for SQLITE databases, similar to MS SQL Server Management Studio
with less features. You can view, edit and add data through it, ALTHOUGH I recommend AGAINST ADDING data through this tool, if you intend to use your database through Core Data. You can even use this tool for SQLITE databases you'll usually create through Terminal (it's what I do when I need to, and when not needing to use MS SQL). Hope this helps you out. If this was helpful, please don't forget to up-vote my post(s). Thanks.