Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 896953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T14:48:45+00:00 2026-05-15T14:48:45+00:00

I have a core data recipe object that contains an ordered list of ingredient

  • 0

I have a core data recipe object that contains an ordered list of ingredient objects.

The ingredients are displayed as a list in a UITableView. When the user cancels editing of the table view, I call rollback on the MOC, which may restore some ingredients (any that the user has deleted) and remove others (any that the user has added). I would like to animate the insertion/deletion so that the transition isn’t jarring.

This is a bit harder than it seems at first, particularly since the UITableView will hack up a hairball if you both insert and remove the same cell.

Is there a bit of sample code out there that would help steer me in the right direction? Right now I have a ridiculously complicated setup using NSMutableSets that isn’t quite working right.

NSMutableArray *preIngredients = [NSMutableArray arrayWithArray:[recipe orderedIngredients]];

[self.recipe.managedObjectContext rollback];

NSMutableArray *postIngredients = [NSMutableArray arrayWithArray:[recipe orderedIngredients]];

NSMutableSet *beforeIngredients = [NSMutableSet setWithArray:preIngredients];
NSMutableSet *afterIngredients = [NSMutableSet setWithArray:postIngredients];

NSMutableSet *restoredIngredients = [NSMutableSet setWithSet:afterIngredients];
[restoredIngredients minusSet:beforeIngredients];

NSMutableSet *removedIngredients = [NSMutableSet setWithSet:beforeIngredients];
[removedIngredients minusSet:afterIngredients];

NSMutableSet *allIngredients = [NSMutableSet setWithSet:beforeIngredients];
[allIngredients unionSet:afterIngredients];

int whatToDo[[preIngredients count]];
for (int i = 0; i < [preIngredients count]; i++)
    whatToDo[i] = 0;

for (Ingredient *ingredient in preIngredients) {
    int row = [preIngredients indexOfObject:ingredient];

    if ([removedIngredients containsObject:ingredient])
        whatToDo[row]--;

    if ([restoredIngredients containsObject:ingredient])
        whatToDo[row]++;
}

for (int i = 0; i < [preIngredients count]; i++) {
    if (whatToDo[i] < 0)
        [rowsToRemove addObject:[NSIndexPath indexPathForRow:i inSection:0]];
    else if (whatToDo[i] > 0)
        [rowsToRestore addObject:[NSIndexPath indexPathForRow:i inSection:0]];
}

    // Also remove the "add new ingredient" cell
NSIndexPath *insertNewCellIndexPath = [NSIndexPath indexPathForRow:[preIngredients count] inSection:0];
if ([rowsToRestore indexOfObjectIdenticalTo:insertNewCellIndexPath] == NSNotFound)
    [rowsToRemove addObject:insertNewCellIndexPath];
else
    [rowsToRestore removeObjectIdenticalTo:insertNewCellIndexPath];

[self.tableView insertRowsAtIndexPaths:rowsToRestore withRowAnimation:UITableViewRowAnimationTop];
[self.tableView deleteRowsAtIndexPaths:rowsToRemove withRowAnimation:UITableViewRowAnimationTop];
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T14:48:45+00:00Added an answer on May 15, 2026 at 2:48 pm

    Here is the working code, making heavy use of sets. If you know of a way to simplify it, don’t hesitate to speak up 🙂

    [self.tableView beginUpdates];
    
    NSMutableArray *preIngredients = [NSMutableArray arrayWithArray:[recipe orderedIngredients]];
    [self.recipe.managedObjectContext rollback];
    NSMutableArray *postIngredients = [NSMutableArray arrayWithArray:[recipe orderedIngredients]];
    
    NSMutableSet *beforeIngredients = [NSMutableSet setWithArray:preIngredients];
    NSMutableSet *afterIngredients = [NSMutableSet setWithArray:postIngredients];
    
    NSMutableSet *ingredientsToRestore = [NSMutableSet setWithSet:afterIngredients];
    [ingredientsToRestore minusSet:beforeIngredients];
    
    NSMutableSet *ingredientsToRemove = [NSMutableSet setWithSet:beforeIngredients];
    [ingredientsToRemove minusSet:afterIngredients];
    
    NSMutableSet *indexPathsToRestore = [NSMutableSet setWithCapacity:[ingredientsToRestore count]];
    NSMutableSet *indexPathsToRemove = [NSMutableSet setWithCapacity:[ingredientsToRemove count]];
    
    for (Ingredient *ingredient in ingredientsToRemove)
        [indexPathsToRemove addObject:[NSIndexPath indexPathForRow:[preIngredients indexOfObject:ingredient] inSection:0]];
    
    // Also remove the "add new ingredient" row
    [indexPathsToRemove addObject:[NSIndexPath indexPathForRow:[preIngredients count] inSection:0]];
    
    for (Ingredient *ingredient in ingredientsToRestore)
        [indexPathsToRestore addObject:[NSIndexPath indexPathForRow:[postIngredients indexOfObject:ingredient] inSection:0]];
    
    NSMutableSet *commonIndexPaths = [NSMutableSet setWithSet:indexPathsToRemove];
    [commonIndexPaths intersectSet:indexPathsToRestore];
    
    [indexPathsToRemove minusSet:commonIndexPaths];
    [indexPathsToRestore minusSet:commonIndexPaths];
    
    [self.tableView insertRowsAtIndexPaths:[indexPathsToRestore allObjects] withRowAnimation:UITableViewRowAnimationTop];
    [self.tableView deleteRowsAtIndexPaths:[indexPathsToRemove allObjects] withRowAnimation:UITableViewRowAnimationTop];
    
    [self.tableView endUpdates];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Core Data object that contains an NSSet of other objects (e.g.
I have a Core Data List entity that has an ordered relationship to ListItems.
I have a core data context with objects that have a date stored in
I have a core data 'ShoppingList' which contains 'Item' objects. I store a display
I have a Core Data based application that stores hierarchal data displayed using a
Say I have core data objects of type obj that has a property propertyA
I have a Core Data based mac application that is working perfectly well until
I have several Core Data model version that I have added to my .xcdatamodeld
I have a core data value that is integer 32. How should I set/get
I have a core data application that uses an NSPredicate in one of its

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.