Alternative to overriding hasChangeValueForKey: in NSManagedObject

I have a managed entity that acts like a playlist, it has a lot to do with playlist articles. There may be several playlists, but only one β€œactive” playlist. An active playlist is indicated by the logical attribute of the managed entity.

I have the number of items in the active playlist, displayed as an icon in the tab bar item. The view controller, which represents the tab bar item, listens for a specific notification that is triggered when the contents of the active playlist are updated.

I implemented this in what, in my opinion, is an awkward way and would like to do it better. It works at the moment, but I'm not happy with it.

Currently, every playlist object in awakeFromFetch checks to see if it is active, and if it registers itself (using observing the key value) as an observer for the key path, which is the key for the relationship. When he notices a change, he triggers a notification that causes the tab bar item to update.

If a playlist loses or receives an active state, it stops or starts to monitor itself properly, so notifications are activated only from the active playlist.

I would like to discard all the KVO code that I myself observe, since I am concerned about the various entry and exit points, as well as when you need to add and remove observers accordingly. It seems too dirty.

I would just like to override didChangeValueForKey: check and send my notification there, if necessary, and then call the super implementation. But this is expressly prohibited in the documentation:

didChangeValueForKey:

Called to inform the recipient that the value of this property has changed.

- (void) hasChangeValueForKey: (NSString *) key

Parameters

key

The name of the changed property. Discussion For more information, see Key Value Observation Programming Guide.

You should not override this method.

So what can I do?

+4
source share
3 answers

I solved this by creating a separate object (one of which I use to control my underlying data stack) instead of an observer. All the difficulties associated with self-exploration have now disappeared, and I do not need to worry about adding or removing observers on awakeFromFetch, etc.

0
source

What you want is an observation of key values. You should be able to register for this particular key and receive a notification when it changes. Check it out as well: Using KVO to watch for changes in object properties within a collection in Objective-C

0
source

I just read the same documentation, but if you look at the top of the NSManagedObject documentation, it actually says: "You are very discouraged ..."

I think it all depends on your implementation details. For example, I do the following in a data model that I can change locally and synchronize with the server:

 - (void)didChangeValueForKey:(NSString *)key { [super didChangeValueForKey:key]; // MUST CALL THIS! if ([key isEqualToString:NSStringFromSelector(@selector(name))] || [key isEqualToString:NSStringFromSelector(@selector(text))] || [key isEqualToString:NSStringFromSelector(@selector(filename))] ) { self.lastModified = [NSDate date]; } } 

I'm not sure why this will be considered bad. He simply says: "Do what you usually do. In addition, I would like to set another property that depends on this change."

0
source

Source: https://habr.com/ru/post/1400579/


All Articles