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

  • Home
  • SEARCH
  • 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 8649751
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:38:19+00:00 2026-06-12T13:38:19+00:00

I have a Client that has a to-many-relationship to Invoice , the property is

  • 0

I have a Client that has a to-many-relationship to Invoice, the property is called invoices. Now I wrote a custom read-only property latestInvoice which I want to observe in my interface:

- (MyInvoice *)latestInvoice
{
  NSArray *invoices = [self valueForKey:@"invoices"];
  NSSortDescriptor *sortDescriptor = [NSSortDescriptor
    sortDescriptorWithKey:@"date" ascending:NO];

  return invoices.count
    ? [invoices sortedArrayUsingDescriptors:@[sortDescriptor]][0]
    : nil;
}

I register Client as an observer for invoices:

- (void)dealloc
{
  [self removeObserver:self forKeyPath:@"invoices"];
}

- (void)registerObservers
{
  [self addObserver:self forKeyPath:@"invoices" options:
    (NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld)
    context:NULL];
}

- (void)awakeFromInsert
{
  [super awakeFromInsert];

  [self registerObservers];
}

- (void)awakeFromFetch
{
  [super awakeFromFetch];

  [self registerObservers];
}

And I manually post change notifications:

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object
  change:(NSDictionary *)change context:(void *)context
{
  if ([keyPath isEqualToString:@"invoices"])
  {
    [self willChangeValueForKey:@"latestInvoice"];
    [self didChangeValueForKey:@"latestInvoice"];
  }
}

Is this the right / bug-free / preferred way to do observe Core Data properties that depend on a relationship or do I abuse the framework?

  • 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-12T13:38:20+00:00Added an answer on June 12, 2026 at 1:38 pm

    Register latestInvoice as a dependent key of invoices.

    + (NSSet *)keyPathsForValuesAffectingLatestInvoice {
        return [NSSet setWithObjects:@"invoices", nil];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Background - I have a client that has an Oracle database with many procedures
I work at a company that has many clients that have their own website
I have a client that has a PostgreSQL database and he cannot remember the
I have a client that has an app built with django. On every page
I have a client project that has posts assigned to their country of origin.
We have a Japanese client that has source code in COBOL on an mainframe.
I have a web service client that has an Authenticator class. The Authenticator requires
I have a client apllication that run in very restricted area - it has
Hi I use GWT and I have a com.smartgwt.client.widgets.Button that has the following eventHandler:
Good Afternoon, I have a html aspx page that has many many div elements.

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.