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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T00:49:23+00:00 2026-06-02T00:49:23+00:00

I have a question regarding core data and notifications. After reading Apple docs, I

  • 0

I have a question regarding core data and notifications. After reading Apple docs, I didn’t find the answer.

This is the problem:
I have a managed object with a one-to-many relationship.
I would like to know when an object has been added or removed from this relationship.
(A “Category” has many “Items”)
I use this to know when item’s category are added or removed:

[category addObserver:self forKeyPath:@"items" 
   options:NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew 
   context:nil];

And then this method is called:

- (void)observeValueForKeyPath:(NSString *)keyPath 
   ofObject:(id)object change:(NSDictionary *)change context:(void *)context;

The problem:
Let’s say a category has 2 items, and I delete one of them. Now the category has only 1 item.
However, if I launch a fetched request from observeValueForKeyPath, it says the category has 2 items, but that is wrong, because it has now 1.

I guess this is because observeValueForKeyPath is called while context is being saved, [context save:&error] (that’s the only thing about this I found in Apple docs), and perhaps the modifications are not effective until [context save:&error] line is ended.
So, to test my theory, I modify my observeValueForKeyPath code, and I moved all code there inside a method, and I used:

 [self performSelector:@selector(launchFetchedRequest) 
   withObject:nil afterDelay:0];

in order to execute that method the next run loop, that is, after [context save:&error]. And voilá, everything works fine now.

So, if I want to launch a fetch request inside observeValueForKeyPath, do I have to launch it the next run loop? Is this a normal behavior? How do you do this usually?

I’d like to know your thoughts about this topic. Thanks in advance.

  • 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-02T00:49:25+00:00Added an answer on June 2, 2026 at 12:49 am

    Ricardo,

    Use the object parameter. It is your category collection. All you have to do is look at what’s in it, so you don’t really need to run a query and hit the managed object context.

    The problem here is that the notification event is fired as the collection changes (KVO) and uses another thread loop, and the main thread has not propagated the change into the managed object context yet. If you used the object parameter, it should be correct.

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

Sidebar

Related Questions

I have a question regarding the way you create relationships in Core Data. I
I have a basic question regarding populating Core Data with data. I am building
I have a question in core data: there are 2 Entities in the project,
I can't find a clear description of these steps in Apple docs... I have
I have question regarding the SQLAlchemy. How can I add into my mapped class
I have question regarding the use of function parameters. In the past I have
I have question regarding disabling browser caching. I have already found few solutions, and
I have a question regarding dependency injection. say i want to create a class
I have a question regarding static function in php. let's assume that I have
I have a question regarding the onkeypress event on JavaScript. Is it possible to

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.