how do i correlate a KVO with a particular object?

Home Forums Syphon Syphon Development – Developer how do i correlate a KVO with a particular object?

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #5158
    Brian Chasalow
    Participant

    If i want to know which index in my SyphonServerDirectory serversArray caused a KVO event,

    - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
    {
    	NSLog(@"Changes happened in Syphon Client : %@ change:%@", object, change);
    	if([keyPath isEqualToString:@"servers"])
    	{
             //what should i test for here?
    	}		
    
    }
    #5159
    bangnoise
    Keymaster

    If you register to receive notifications before the change as well as after (using the NSKeyValueObservingOptionPrior option), the change dictionary should contain the index of the item which will be be removed (for removals) – and at that point .servers will still contain the about-to-go server. For additions and name-changes, the change dictionary after the change should have the index of the changed item(s).

    That said, depending what you’re doing, it might be easier to register for SyphonServerDirectory’s NSNotifications (SyphonServerAnnounceNotification, SyphonServerUpdateNotification and SyphonServerRetireNotification) and not use KVO at all, as the notifications pass you the serverDescription for the changing server as their notification object.

    #5160
    Brian Chasalow
    Participant

    I’m ok with just receiving notifications after the event. my question i suppose had more to do with what the ‘object’ and ‘change’ were referring to. I know just enough objective-c to get myself into trouble.

    why does the indexes key in the ‘change’ NSDictionary* refer to an NSIndexSet and not an NSNumber? Is it because there could be multiple changes happening in the same frame?

    #5161
    Brian Chasalow
    Participant

    Just got it working with the Announce/Retire/Update notifications as well- the problem is really that there isn’t a good way to perform a C# callback in unity from C++ plugins.

    The way I’ve been dealing with that previously is just polling a bool that represents ‘did something change?’ that is triggered by the KVO. This is not very explicit, which is a shame, as I’d like to perform different tasks in Unity based on whether it was an Announce/Retire/Update, while at the same time performing the least amount of C# -> C++ interop; this is more a design issue than a code one i suppose.

    #5162
    bangnoise
    Keymaster

    my question i suppose had more to do with what the ‘object’ and ‘change’ were referring to.

    NSKeyValueObserving Protocol Reference

    I’m ok with just receiving notifications after the event.

    Receiving them prior to the event would be necessary if you wanted to discover details of a retiring server via KVO, which is why I mentioned it (after the event it has gone). However it sounds like the NSNotifications route might be more useful.

    why does the indexes key in the ‘change’ NSDictionary* refer to an NSIndexSet and not an NSNumber? Is it because there could be multiple changes happening in the same frame?

    yep, exactly.

    The way I’ve been dealing with that previously is just polling a bool that represents ‘did something change?’ that is triggered by the KVO. This is not very explicit, which is a shame, as I’d like to perform different tasks in Unity based on whether it was an Announce/Retire/Update, while at the same time performing the least amount of C# -> C++ interop; this is more a design issue than a code one i suppose.

    Use three bools? 😉

    Yep I don’t know enough (anything) about Unity to make any suggestions really – if you’re not allowed to/can’t post state changes outside calls into your plugin then you’ll just have to accumulate them like you are. Sorry, not very helpful!

    #5163
    Brian Chasalow
    Participant

    I actually found a way to do objective-c callbacks to Unity directly from the plugin, so i’m in the process of reworking the system to use NSNotifications instead of KVO. If i use NSNotifications, will they post the notification prior to a server retirement similar to the KVO option?

    here is the guide i followed: http://www.tinytimgames.com/2010/01/10/the-unityobjective-c-divide/

    but i had to make a bunch of changes after reading the embedded mono docs, will detail them once I get my shit together and make it all work how it should. It’s some voodoo shit that involves searching the compiled assembly for methods by name, caching those methods and invoking their constructors.

    #5164
    bangnoise
    Keymaster

    Yikes, looks… non-trivial.

    If i use NSNotifications, will they post the notification prior to a server retirement similar to the KVO option?

    The server may be long-gone in either case – the remote app doesn’t wait for the notification to be received, and sometimes you’ll receive a retirement notification when the SyphonServerDirectory notices a server has died long after the event. With NSNotifications (or a pre-change KVO) you will get a server description dictionary with details of the departed server though.

Viewing 7 posts - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.