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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T17:41:42+00:00 2026-06-16T17:41:42+00:00

UPDATE : Martin R below has provided a very clear (and succinct!) answer that

  • 0

UPDATE: Martin R below has provided a very clear (and succinct!) answer that almosts answers my question. I suppose I should rephrase:

Can you think of any reasons why fetching NSManagedObjects that are connected to another NSManagedObject wouldn’t work when calling prepareForDeletion, given that such objects are connected one-to-one to the object being deleted?

I need to be able to call prepareForDeletion so that I can run some entity checking before deciding whether I should delete an object’s children.


Are there any tricks to deleting an object’s children objects in Core Data when it is a one-to-one relationship?

I have a rather complex Core Data model wherein deleting a single NSManagedObject should also delete its children NSManagedObjects via prepareForDeletion. Running a series of debugging NSLog statements immediately prior to deletion reveals that all relationships have been set up properly. However, when actually attempting to delete the object, it seems many (though not all) of these relationships have been lost, as attempting to fetch some (but again, not all) of these objects via NSFetchRequest returns empty arrays.

I can’t seem to figure out any differences between how I fetch the objects that are found versus the objects that are not found, other than that those that are found are inverse to-many relationships while those that cannot be found are one-to-one relationships :-/

To delete the “main” object I simply call [managedObjectContext deleteObject:mainObject];, and within the main object’s private API I have overwritten prepareForDeletion as follows:

- (void)prepareForDeletion
{
//    [super prepareForDeletion]; // commented but uncommenting doesn't change results
    [MyDataManager deleteChildOneForMainObject:self];
    [MyDataManager deleteChildrenTwoForMainObject:self];
    [MyDataManager deleteChildrenThreeForMainObject:self];
}

where MyDataManager is a custom NSObject containing only class methods. MyDataManager then searches for corresponding NSManagedObjects in the managed object context via something akin to the following:

- (BOOL)deleteChildOneForMainObject:(MainObject *)mainObject
{
    NSFetchRequest *fetch = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"ChildOne" inManagedObjectContext:managedObjectContext];
    [fetch setEntity:entity];
    [fetch setPredicate:[NSPredicate predicateWithFormat:@"(mainObject == %@)", mainObject]];
    NSError *error;
    NSArray *childOnesToDelete = [managedObjectContext executeFetchRequest:fetch error:&error];
    if (childOnesToDelete.count > 1)
    {
        NSLog(@"[WARNING] More than one ChildOne for mainObject found; deleting all");
    }
    NSLog(@"[TEST] Deleting %i ChildOnes", childOnesToDelete.count);
    for (ChildOne *childOne in childOnesToDelete)
    {
        [managedObjectContext deleteObject:childOne];
    }
    if ([managedObjectContext save:&error]) return YES;
    else NSLog(@"[WARNING] Save error for function [deleteChildOneForMainObject:]");
    return NO;
}

Again, each NSManagedObject of type “MainObject” has a one-to-one relationship to “ChildOne” and one-to-many relationships with “ChildTwo” and “ChildThree”. “Child One,” “ChildTwo,” and “ChildThree” all have to-one relationships with “MainObject.”

  • 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-16T17:41:43+00:00Added an answer on June 16, 2026 at 5:41 pm

    So I finally got IRC to work for me! and a helpful person on the #iphonedev channel provided these insights:

    • (Like Martin R said) I should put up with the visual interface for constructing my Core Data model and use the provided “Delete Rule” dropdown menus to create my deletion logic. (In the “Utilities” right-side pane, third tab that looks like a wedge of cheese.) “Cascade” and “Nullify” are the most useful values.

    • “Delete Rules” are one-way and unique to each relationship, so you have a fair degree of control.

    • Instead of fetching an NSManagedObject that has a one-to-one relationship with a provided NSManagedObject, just use the property. Using a fetch as a “safeguarding” mechanism is stupid and unnecessary since this relation is one-to-one by definition. So if I have Ying and Yang objects, I should just perform [managedObjectContext deleteObject:ying.yang] or [managedObjectContext deleteObject:yang.ying].

    Ultimately prepareForDeletion was only relevant with regards to cascading deletion as per Martin R’s answer.

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

Sidebar

Related Questions

Update is below code chunks. Original Question: I have an input field that is
Update : I found almost exact similar question , yet it has slightly different
The code below has (at least) two problems: the Copy button doesn't update the
Question Answer below Hello I'm looking a simple way to change the colour of
UPDATE I solved my own question. See below. I made a site with bootstrap
UPDATE: I've been playing around with this more, and it seems like tmux's clear-history
Update : This is no longer an issue from C# 6, which has introduced
UPDATE UPI_ATTRIBUTE SET SITE_INC ='0' WHERE USER_PROFILING_NAME IN ('CAR_IMPLICIT','CAR_EXPLICIT') Above is my query that
UPDATE 6/21/12 I have a form in rails that is working similar to an
Below is the quote of answer by Mykhaylo Adamovych on topic How to add

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.