how to notify NSFetchRequestController if an attribute of an object in relationship chain is changed

StackOverflow https://stackoverflow.com/questions/23627308

  •  21-07-2023
  •  | 
  •  

Question

enter image description here

Consider the above model, I have an FRC (NSFetchRequestController) on Items. I want FRC to update view, if Person is changed.

I know I can observe NSManagedObjectContextObjectsDidChangeNotification for changes, but this doesnt look intuitive.

I was thinking calling willChangeValueForKey and didChangeValueForKey will do the trick but it didnt worked as expected.

            Person *person = [NSEntityDescription
                                  insertNewObjectForEntityForName:@"Person"
                                  inManagedObjectContext:temporaryContext];

            person.name= [msg objectForKey:@"name"];


//            [?? willChangeValueForKey:@"friendPhoneNumber"];
//            [?? didChangeValueForKey:@"friendPhoneNumber"];

            // Save the context.
            NSError *error = nil;

            if (![temporaryContext save:&error]) {
                NSLog(@"Error in Saving MOC: %@",[error description]);
            }

            [managedObjectContext performBlock:^{
                // Save the context.
                NSError *error = nil;
                if (![managedObjectContext save:&error]) {
                    DLog(@"Error in Saving MOC: %@",[error description]);
                }

                [writerObjectContext performBlock:^{
                    // Save the context.
                    NSError *error = nil;
                    if (![writerObjectContext save:&error]) {
                        DLog(@"Error in Saving MOC: %@",[error description]);
                    }else{
                        [writerObjectContext refreshObject:changedObjectId mergeChanges:YES];
                    }

                }]; // writer
            }]; // main
        }]; // parent
Was it helpful?

Solution

I wanted to notify FRC on items that person has been modified, but it didnt worked as expected. FRC doesnt register any change.

A NSFetchedControllerregisters changes only for the entity is associated with. As you described, that is the Item entity. If a Person changes, the fetched controller will not respond to changes.

If you want to repond to changes, you need to do it manually. An example explanation can be found at NSFetchedResultsController pitfall.

A simple question, why the items should change on person modifications? Maybe you should perform a new request...only suppositions.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top