This is my core data structure
Entity – Category
Attr – Id(NSString) primary key, name(NSString)
Entity – Product
Attr – Id(NSString), name(NSString), parentCategoryId(NSString) foreign key,….
As can be seen from above, Id from Category points to parentCategoryId from Product.
In this case, I want to use name from category associated with concerned parentCatrgoryId from product as my section name for UITableView.(I use @”parentCategoryId” as key to NSSortDescriptors in fetchrequest)
But parentCatrgoryId are simply alphanumeric texts.
How can I decode the the alphanumeric text i.e. parentCatrgoryId from product to name from category
EDIT
This is what I did & its working. But what I want to ask if what I did is efficient.
I added one more attribute named parentCategoryIdName & used this to store the exact name by computing some simple operation.
.h file
@property (nonatomic, retain) NSMutableArray *categoryArr;
@property (nonatomic, retain) NSMutableArray *productArr;
.m file
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Category" inManagedObjectContext:self.MOContext];
// Setup the fetch request
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entity];
// [request setPredicate:[NSCompoundPredicate andPredicateWithSubpredicates:subpredicates]];
NSError *error;
NSMutableArray *mutableFetchResults = [[self.MOContext executeFetchRequest:request error:&error] mutableCopy];
[self setCategoryArr: mutableFetchResults];
NSEntityDescription *entity2 = [NSEntityDescription entityForName:@"Product" inManagedObjectContext:self.MOContext];
// Setup the fetch request
NSFetchRequest *request2 = [[NSFetchRequest alloc] init];
[request2 setEntity:entity2];
// [request2 setPredicate:[NSPredicate predicateWithFormat:@""]];
NSError *error2;
NSMutableArray *mutableFetchResults2 = [[self.MOContext executeFetchRequest:request2 error:&error2] mutableCopy];
[self setProductArr: mutableFetchResults2];
To insert the data in DB
for (int i=0; i< [self.productArr count]; i++) {
Product *productEnt = [self.productArr objectAtIndex:i];
NSManagedObjectContext *context = self.MOContext;
for (int j=0; j<self.categoryArr.count; j++) {
Category *categroy = [self.categoryArr objectAtIndex:j];
if ([categroy.objectId isEqualToString:productEnt.parentCategoryId]) {
[productEnt setParentCategoryIdName:categroy.name];
}
// Save the context.
NSError *error = nil;
if (![context save:&error]) {
// Replace this implementation with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
}
to recap:
From what I gather you need the id/name to form sections and show it in the section’s title.
you solution would be efficient IMO but it should be the last resort:
ideas:
I would get the categoryName from categoryId JIT when you have to return the
titleForSectionAtIndex:
OR if that is too slow.
fetch all section names when reloading the results.
=> Dont keep ’em in the database and duplicate data
BUT
if you see that it is really too slow, THEN denormalizing your data in that way is good IMO (it shoule be the last solution to consider)