开发者

How to add data manually in core data entity

开发者 https://www.devze.com 2022-12-26 10:28 出处:网络
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 data (you can say i want to 开发者_JS百科add data in a table).

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 data (you can say i want to 开发者_JS百科add data in a table).

Earlier when I was using Sqlite, I would add data using the Terminal. But here in Core Data I am not able to find a way to manually add data. I just want to add data to the Context 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 programmatically. But I don't need to do it programmatically. I want to do it manually.


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).


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.


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/


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消