Reputation: 995
I am studying iOS development with Objective-C. I have a plist with a lot of data and want to import them into the application. This plist consist of a store containing: location (mapped as an entity), court (mapped as an entity), title, subtitle, address, phone number and category (mapped as an entity). I've been researching and found that one of the solutions is to use CoreData + MagicalRecord.
Here is my mapping (may suggest changes):
After selecting the neighborhood, you have a category table (which was supposed to be the category entity), this table is a "subdivision" of main plist. After he select the category, the application take a second table which is shown every store that contains the category clicked.
In my CategoriaViewController (CategoryViewController) I have a method to load all categories of all the stores, but do not know how to use MagicalRecord to save the plist the categories in the Category entity and show it on the table. In my method I even instantiate the category entity, but do not know how to use it.
Here's my method (translated):
- (void) loadCategories {
self.category = [Category MR_createEntity];
self.listOfCategories = [[NSMutableArray alloc] init];
NSString *path = [[NSBundle mainBundle] pathForResource:@"Category" ofType:@"plist"]
NSDictionary *myPlist = [NSDictionary dictionaryWithContentsOfFile:path];
self.listOfCategories = [myPlist objectForKey:@"categories"];
}
And here's my full class.
Upvotes: 0
Views: 63
Reputation: 80271
Your confusion arises from the fact that you think an entity is a table. But no, it is just an object in the object graph. You have to call MR_createEntity
for each object you want to insert.
[MagicalRecord saveWithBlock:^(NSManagedObjectContext *localContext) {
for (NSString *name in listOfCategories) {
Category *newCategory = [Category MR_createEntity];
newCategory.name = name;
}
}
Put the whole thing into a saveWithBlock
block to save it to the store in the background.
Upvotes: 1