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 7872199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T02:08:12+00:00 2026-06-03T02:08:12+00:00

I have a core data model defined as follows: A User has many events.

  • 0

I have a core data model defined as follows:

A User has many events. Each event can have many pictures. The relationships have “cascade” delete rule.

I’m trying to understand how to delete local files when the entity disappears. Is there some kind of dealloc or “finalize” method that a core data entity calls before it is gone?

Each picture entity has a reference to a local file that’s stored in the app’s documents directory.

When the user is deleted through table view’s commitEditingStyle, I can delete the images by stepping through the relationships and delete the files manually:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{

    {
        // Delete the managed object for the given index path
        NSManagedObjectContext *context = [[[RKObjectManager sharedManager] objectStore] managedObjectContext];

        AppUser* managedObject = [self.fetchedResultsController objectAtIndexPath:indexPath];

        //clean up local files before deleting the object
        [self deleteLocalContentForAppUser:managedObject];        

//now delete the object
        [context deleteObject:managedObject];

        // Save the context to remember deletion
        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();
        }

    }

}

However, when the entities are deleted through other methods, and my fetched results controller gets notified of them, the nested relationships contain no objects. The user’s events set will not have any entities to step through and delete the local content. Is this the result of the “cascade” delete rule set for the relationship?

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
       atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
      newIndexPath:(NSIndexPath *)newIndexPath
{
//    UITableView *tableView = self.tableView;
//     
    AppUser* managedObject = anObject;
    switch(type) {
        case NSFetchedResultsChangeInsert:
            [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationNone];
//             [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
            //             [self configureCell:[tableView cellForRowAtIndexPath:newIndexPath] atIndexPath:newIndexPath];
            break;

        case NSFetchedResultsChangeDelete:
            [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];


            //these methods cannot find any content to delete
            [managedObject deleteLocalImages];
            [managedObject deleteLocalContent];
            break;

        case NSFetchedResultsChangeUpdate:
            [self configureCell:[self.tableView cellForRowAtIndexPath:newIndexPath] atIndexPath:newIndexPath];

            [managedObject updateLocalImages];
            break;

        case NSFetchedResultsChangeMove:
            [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
            [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]withRowAnimation:UITableViewRowAnimationFade];
            break;
    }
}
  • 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-06-03T02:08:13+00:00Added an answer on June 3, 2026 at 2:08 am

    You can implement the prepareForDeletion method in your NSManagedObject subclass. It is invoked automatically before an object is deleted, so that you can also delete referenced files from there.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this Core Data Model Hierarchy, with the respective relationships to each other:
In my core data model I have a Person entity that has a to
In my Core Data model I have two entities: List and Patient. List has
I have a core data model that has two entities, Bid and Result. I
I have a Core Data model entity NoteObject that has a transformable type arrayOfTags
I have a Core Data model with two entities: A and B. A has
Hi I have a evolving core data model setup as follows: lookup -> detail
I have a core data model which includes an entity with a to-many relationship.
I have a Core Data model setup like so: Blockbuster Entity To-Many relationship to
I have a Core Data model which has three entities: A, B, and C.

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.