简体   繁体   中英

Inter-Model/NSManagedObject communication

I have two NSManagedObject subclasses: Parent and Child. Parent contains many Child(ren) in an OrderedSet. When state changes in a Child I want the parent to know about it.

If I was programming in another language I might use events, having the Parent listening for events from each of its children, however given that target-action is limited to view components, all Objective C offers me is use of a global NSNotificationCenter. I definitely don't like the idea of a Model tapping into global notifications (listening directly via events is Ok in my book), so it seems my only alternative is Delegation. However using delegation between two NSManagedObjects seems like a dangerous idea, given the difficulty in ensuring one party does not lose its reference to the other.

Does anyone have any suggestions as to how I should be handling this?

Another option is key-value observing. Set it up like this:

const static void *kParentObservingChildSomePropertyContext = &kParentObservingChildSomePropertyContext;

[child addObserver: child.parent forKeyPath: @"someProperty" options: /*see below*/ context: kParentObservingChildSomePropertyContext];

the weird constant definition exists because every observing context should be unique so that you don't tread on superclass or subclass observation contexts. To see what options you need to set, consult the manual and compare with your specific needs. Now whenever that property on your child object changes, your parent will receive:

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

Check that you have the correct context for your observation, then handle the change. If you get a different context here, forward the message to super .

When you're done observing the path, you remove the parent as an observer of the child:

[child removeObserver: child.parent forKeyPath: @"someProperty" context: kParentObservingChildSomePropertyContext];

Use the same context pointer you used in -addObserver:forKeyPath:options:context so that the correct instance of the observation is removed.

However using delegation between two NSManagedObjects seems like a dangerous idea, given the difficulty in ensuring one party does not lose its reference to the other.

Delegating, observing and watching for notifications from specific objects all suffer from this problem. You need to make sure that the lifetime of your interest in the notifications matches the lifetimes of the objects involved, otherwise you could easily "leak" observation info or - and this is worse - send notifications to stale object pointers. None of these solutions is immune to that, though in the case of the Delegate pattern you can use a zeroing weak reference to ensure that when the parent object disappears, the child will no longer try to delegate to it.

I pick a field to watch in the view controller that will service the request... [self addObserver:self forKeyPath:@"clientProgress.dateLastUpdated" options:0 context:nil];

I make sure in that same view controller that I remove the observer on dealloc (or whatever passes for dealloc in ARC). - (void)dealloc { [self removeObserver:self forKeyPath:@"clientProgress.dateLastUpdated"]; }

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if ([keyPath isEqual:@"clientProgress.dateLastUpdated"]) {
    // If this key path has changed, the browser needs to update its display.
    NSError *error = nil;
    if (![managedObjectContext save:&error]) {
        UIAlertView *dialog = [[UIAlertView alloc] initWithTitle:@"Save Error" message:@"Error saving inserted record, contact Tech Support" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
        [dialog show];
        [dialog release];
        exit(-1);  // Fail
    }
    if ( changeIsComingFromLibraryInsert ) {

    }
    [conditioningTableView reloadData];
}
// Essential to call super class implementation - NSArrayController relies heavily on KVO
//[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];

}

Finally, when I want to call the observer, I merely update the date... // force a save by setting the modified date and the preceeding ViewController with then save and reload from the calling view controller // as there is an observer running watching this key value clientProgress.dateLastUpdated = [NSDate date];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM