How to add data manually to the main data object

I am working on Core Data for the first time. I just created Entity and Attributes for this object. I want to add some rows as data (you can say that I want to add data to the table).

Previously, when I used Sqlite , I would add data using Terminal . But here, in Core Data , I cannot find a way to add manually . I just want to add data to Context and display it in a UITableView . I looked at the Core Data documentation, but does not explain how to add data manually, although it does explain how I can add it programmatically. But I do not need to do this programmatically. I want to do it manually.

+7
iphone core-data
source share
4 answers

I am also pretty new to CoreData and have been looking for a solution for this. I was happy to find an article here: http://iphoneinaction.manning.com/iphone_in_action/core-data on how to get a CSV file and import your data (look at Core Data, Part 5: Prefilling data).

Here is the code I'm using:

 -(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 don't need all the code. Here is the code from the article. If that doesn't make sense, try a look at the article.

CSV file:

 1,A$20,Australian Dollars,20,aussie-20.png 2,R$20,Brazilian Reals,20,brasil-20.png 

the 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: 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 find the Add-On tool named SQLITE MANAGER . This will allow you to open any SQLITE database, even created by your application. 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, WHAT I recommend to CONTINUE ADD data with this tool if you intend to use your database through Core Data. You can even use this tool for SQLITE databases that you usually create through Terminal (this is what I do when it is needed, and when you do not need to use MS SQL).

+7
source share

You add data to an object without the corresponding custom subclass of NSManagedObject as follows:

 NSManagedObject *mo = [NSEntityDescription insertNewObjectForEntityForName:@"MyEntityName" inManagedObjectContext:aManagedObjectContext]; [mo setValue:aValue forKey:@"aKeyName"]; id aValue=[mo valueForKey:@"aKeyName"]; 

Master data is not a tabular database. This is a graph management system for objects. Thus, you process data in Core Data by changing the attributes of objects.

In the above example, I am changing the value stored in the mo instance, which is a shared NSManagedObject. Since mo is a generic NSManagedObject, I use setValue:forKey to store the value in the NSManagedObject associative store. Key names are defined by entities created in the data model.

Most often, you should create a dedicated subclass of NSManagedObject whose attributes are the attributes and relationships of the object. In this case, the code above will look like this:

 MyManagedObjectSubclass *myMO = [NSEntityDescription insertNewObjectForEntityForName:@"MyEntity" inManagedObjectContext:aManagedObjectContext]; myMo.attributeName=aValue; id anotherValue=myMo.attributeName; 

Trying to think about Core Data in terms of SQL will only lead to grief. Master data does not work like SQL. It works as a very interconnected set of user objects. You need to think in objects for Core Data, not tables.

+3
source share

The best solution for you and users of your application is probably to use xml data. You can easily request XML from a web service and save it in your application and not read in real time on the Internet. Of course, if your data is massive, this can be a problem, but you can also break the data requests into smaller chunks and simply save what you retrieve for reuse and possibly offline.

This tutorial from Björn Sållarp should help a lot. http://blog.sallarp.com/iphone-core-data-uitableview-drill-down/

0
source share

I just created NSManagedObjectContext , thus creating a SQLite file in the application folder.

Then I could fill out the SQLite file just like a regular one, and CoreData could read the pre-populated data.

0
source share

All Articles