What I Know
1: During a transaction or a view I am able to delete records from a single entity(table) --Success :)
What I am trying to do
In the code for a category view When I delete a category I want the corresponding Inventory Item to be deleted ;
I know I can set some relationship for this delete , but I was unable to find a good link or a example which does that , I am unable to apply that rule
Here's my code
(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
PIMCategoryListCellTableViewCell *cell = (PIMCategoryListCellTableViewCell*) [tableView cellForRowAtIndexPath:indexPath];
if (editingStyle == UITableViewCellEditingStyleDelete) {
// [self.tblView beginUpdates];
NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
//Records from Category entity(table) deleted -- Success So far
[context deleteObject:[self.fetchedResultsController objectAtIndexPath:indexPath]];
NSError *error = nil;
if (![context save:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
// Use the fetch result controller to get the corresponding item of same category
self.fetchedResultsControllerCategory = nil;
[self fetchedResultsControllerItem:cell.categoryName.text];
NSUInteger count = [self.fetchedResultsControllerCategory.fetchedObjects count];
//this way I know if any item are present for the category
if (count> 0)
{
NSManagedObjectContext *contextN = [self.fetchedResultsControllerCategory managedObjectContext];
NSIndexPath *i;
for ( i.row =0 ; i==1;i++ )
{
[contextN deleteObject:[self.fetchedResultsControllerCategory ob ];
}
// @property (strong, nonatomic) id detailItem;
// [contextN deleteObject:[self.fetchedResultsControllerCategory ob ];
//[contextN reset]
NSError *error = nil;
if (![contextN 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();
// [self.tblView endUpdates];
}
}}
Now I have tried to change the context but still unable to delete the second entity(table) records
Any suggestion , ; Let me know if you need more explanation
You need to set up the relationship in the core data editor.
Get rid of the 'category name' attribute from Inventory, and instead create a relationship to Category. (And an inverse relationship from Category to Inventory)
Then you can set the delete rule on the category relationship to "Cascade" which will then delete any related Inventory, as soon as you delete a category.