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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T09:00:30+00:00 2026-05-14T09:00:30+00:00

My model is setup so Business has many clients, Client has one business. Inverse

  • 0

My model is setup so Business has many clients, Client has one business. Inverse relationship is setup in the mom file.

I have a unit test like this:

- (void)testNewClientFromBusiness
{
    PTBusiness *business = [modelController newBusiness];
    STAssertTrue([[business clients] count] == 0, @"is actually %d", [[business clients] count]);
    PTClient *client = [business newClient];
    STAssertTrue([business isEqual:[client business]], nil);
    STAssertTrue([[business clients] count] == 1, @"is actually %d", [[business clients] count]);
}

I implement -newClient inside of PTBusiness like this:

- (PTClient *)newClient
{
    PTClient *client = [NSEntityDescription insertNewObjectForEntityForName:@"Client" inManagedObjectContext:[self managedObjectContext]];
    [client setBusiness:self];
    [client updateLocalDefaultsBasedOnBusiness];
    return client;
}

The test fails because [[business clients] count] is still 0 after -newClient is called.

If I impliment it like this:

- (PTClient *)newClient
{
    PTClient *client = [NSEntityDescription insertNewObjectForEntityForName:@"Client" inManagedObjectContext:[self managedObjectContext]];
    NSMutableSet *group = [self mutableSetValueForKey:@"clients"];
    [group addObject:client];
    [client updateLocalDefaultsBasedOnBusiness];
    return client;
}

The tests passes.

My question(s): So am I right in thinking the inverse relationship is only updated when I interact with the mutable set? That seems to go against some other Core Data docs I’ve read. Is the fact that this is running in a unit test without a run loop have anything to do with it?

Any other troubleshooting recommendations? I’d really like to figure out why I can’t set up the relationship at the client end.

Update: Some people have suggested I use -processPendingChanges to for the relationships be updated before the end of the run loop where it typically happens. Doing this is not helping me. Another sample test that fails:

- (void)testAssigningRelationship
{
    // BUG: for some unknow reason in this project i have to assign both ends of the relationsip manually.
    NSURL *modelUrl = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%@", [[NSBundle mainBundle] resourcePath], @"PTDataModel.momd/schemaVersion1.mom"]];
    NSManagedObjectModel *managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelUrl]; 

    NSPersistentStoreCoordinator *persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:managedObjectModel];
    [persistentStoreCoordinator addPersistentStoreWithType:NSInMemoryStoreType configuration:nil URL:nil options:nil error:nil];

    NSManagedObjectContext *managedObjectContext = [[NSManagedObjectContext alloc] init];
    [managedObjectContext setPersistentStoreCoordinator:persistentStoreCoordinator];

    PTBusiness *business = [NSEntityDescription insertNewObjectForEntityForName:@"Business" inManagedObjectContext:managedObjectContext];
    STAssertTrue([[business clients] count] == 0, @"is actually %d", [[business clients] count]);
    PTClient *client = [NSEntityDescription insertNewObjectForEntityForName:@"Client" inManagedObjectContext:managedObjectContext];
    //NSMutableSet *clients = [business valueForKey:@"clients"];
    //[clients addObject:client];
    [client setBusiness:business];
    [managedObjectContext processPendingChanges];
    STAssertTrue([business isEqual:[client business]], @"[client business] is %@", [client business]);
    // fails count is 0
            STAssertTrue([[business clients] count] == 1, @"is actually %d", [[business clients] count]);
    NSSet *clientSet = [business valueForKey:@"clients"];
    // fails count is 0
            STAssertTrue([clientSet count] == 1, @"is actually %d", [clientSet count]);
}

I’ve tried to recreate the bug in a fresh project that mirrors my own project’s core data stack, but the fresh projects seem to work correctly. I’m really at a lose where to troubleshoot next. Sure I can work around this is my code (always use the mutableSet) but I worry about this being the tip of a bigger problem.

  • 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-14T09:00:30+00:00Added an answer on May 14, 2026 at 9:00 am

    Thanks for all your help people but the lack of updated inverse relationships seems to have been caused by a very subtle KCV bug in that inside of PTClient I had a relationship called business and a method (for compatibility with a previous implementation) called -isBusiness.

    More info and a sample project demo-ing the bug is on my blog:

    http://blog.clickablebliss.com/2010/04/09/walkthrough-of-a-recent-core-data-bug/

    Thanks again.

    • 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 model setup like so: Blockbuster Entity To-Many relationship to
A client of mine has a dynamic changing business model, they sell products according
I have the following model setup - a User is interested in projects in
i have the following models setup class Player(models.Model): #slug = models.slugField(max_length=200) Player_Name = models.CharField(max_length=100)
I have this setup in my models: class Author(models.Model): name = models.CharField(max_length=100) class Topic(models.Model):
I basically want to setup a relationship similar to Netflix's DVD rental model. There
I have following models setup in my Django application class School(models.Model): name = models.TextField()
In my controller I have model operations that can return empty results. I've setup
I have data coming into my Model, how do I setup to insert the
i have following model setup class Category < ActiveRecord::Base has_ancestry :cache_depth => true, :depth_cache_column

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.