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 rows as da
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 just created an NSManagedObjectContext
, thus creating an SQLite
file in the app folder.
Then I could fill the SQLite file just as a normal one and CoreData
could read out the pre filled data.
I am fairly new to CoreData as well and was looking for a solution for this too. I was happy to find a an article here: http://iphoneinaction.manning.com/iphone_in_action/core-data on how to get a CSV file and import your data (look for Core Data, Part 5: Prefilling Data).
Here is the code I use:
-(void)addData
{
NSString *paths = [[NSBundle mainBundle] resourcePath];
NSString *bundlePath = [paths stringByAppendingPathComponent:@"file.csv"];
NSString *dataFile = [[NSString alloc] initWithContentsOfFile:bundlePath];
NSArray *dataRows = [dataFile componentsSeparatedByString:@"\n"];
[dataFile release];
MyEntity *myMO;
for (int i = 0 ; i < [dataRows count] ; i++)
{
NSLog(@"Added: %d",i);
myMO = (MyEntity *)[NSEntityDescription insertNewObjectForEntityForName:@"MyEntity" inManagedObjectContext:[self managedObjectContext]];
[myMO setAttrib1:[NSNumber numberWithInt:i+1]];
[myMO setAttrib2:[dataRows objectAtIndex:i]];
[self saveAction];
}
}
I only had one column so I didn't need all the code. Here is the code from the article. If it doesn't make sense, try looking at the article.
CSV file:
1,A$20,Australian Dollars,20,aussie-20.png
2,R$20,Brazilian Reals,20,brasil-20.png
Code:
- (void)setupCards {
NSString *paths = [[NSBundle mainBundle] resourcePath];
NSString *bundlePath = [paths stringByAppendingPathComponent:@"cards.csv"];
NSString *dataFile = [[NSString alloc] initWithContentsOfFile:bundlePath];
NSArray *dataRows = [dataFile componentsSeparatedByString:@"\n"];
[dataFile release];
Card *card;
for (int i = 0 ; i < [dataRows count] ; i++)
{
NSArray *dataElements = [[dataRows objectAtIndex:i] componentsSeparatedByString:@","];
if ([dataElements count] >= 4)
{
card = (Card *)[NSEntityDescription insertNewObjectForEntityForName:@"Card" inManagedObjectContext:[self managedObjectContext]];
[card setId:[NSNumber numberWithInt:i]];
[card setName:[dataElements objectAtIndex:1]];
[card setType:[dataElements objectAtIndex:2]];
[card setWorth:[NSNumber numberWithInt:
[[dataElements objectAtIndex:3] intValue]]];
[card setImages:[NSSet setWithObject:
[self setupCardPic:[dataElements objectAtIndex:4]]]];
[self saveAction:self];
}
}
}
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).
The best solution for you and the users of your app is probably to use xml data. You could easily request the XML from a web service and store it in your app and not have to read live from the web all the time. Of course, if your data is massive, this could be a problem but you could also split the data requests into smaller chunks and just store what you fetch for re-use and perhaps an offline mode.
This tutorial by Björn Sållarp should help a lot. http://blog.sallarp.com/iphone-core-data-uitableview-drill-down/