Fixed MYAddressLookup to allocate an NSSet, and to send correct KV notifications. (Based on Jim Roepke's patch, but outsourcing the KV grunge to CollectionUtils.)
     1.1 --- a/Bonjour/MYAddressLookup.m	Tue May 05 22:19:33 2009 -0700
     1.2 +++ b/Bonjour/MYAddressLookup.m	Sun May 10 19:00:50 2009 -0700
     1.3 @@ -25,7 +25,7 @@
     1.4              return nil;
     1.5          }
     1.6          _hostname = [hostname copy];
     1.7 -        _addresses = [[NSMutableArray alloc] init];
     1.8 +        _addresses = [[NSMutableSet alloc] init];
     1.9      }
    1.10      return self;
    1.11  }
    1.12 @@ -62,10 +62,10 @@
    1.13      if (address) {
    1.14          if (flags & kDNSServiceFlagsAdd) {
    1.15              LogTo(DNS,@"%@ got %@ [TTL = %u]", self, address, ttl);
    1.16 -            [_addresses addObject: address];
    1.17 +            kvAddToSet(self, @"addresses", _addresses, address);
    1.18          } else {
    1.19              LogTo(DNS,@"%@ lost %@ [TTL = %u]", self, address, ttl);
    1.20 -            [_addresses removeObject: address];
    1.21 +            kvRemoveFromSet(self, @"addresses", _addresses, address);
    1.22          }
    1.23          [address release];
    1.24      }
    1.25 @@ -96,7 +96,7 @@
    1.26  
    1.27  
    1.28  - (DNSServiceErrorType) createServiceRef: (DNSServiceRef*)sdRefPtr {
    1.29 -    [_addresses removeAllObjects];
    1.30 +    kvSetSet(self, @"addresses", _addresses, nil);
    1.31      return DNSServiceGetAddrInfo(sdRefPtr,
    1.32                                   kDNSServiceFlagsShareConnection,
    1.33                                   _interfaceIndex, 0,
     2.1 --- a/Bonjour/MYBonjourBrowser.m	Tue May 05 22:19:33 2009 -0700
     2.2 +++ b/Bonjour/MYBonjourBrowser.m	Sun May 10 19:00:50 2009 -0700
     2.3 @@ -200,6 +200,7 @@
     2.4  
     2.5  
     2.6  
     2.7 +
     2.8  #pragma mark -
     2.9  #pragma mark TESTING:
    2.10  
    2.11 @@ -221,8 +222,12 @@
    2.12      self = [super init];
    2.13      if (self != nil) {
    2.14          _browser = [[MYBonjourBrowser alloc] initWithServiceType: @"_presence._tcp"];
    2.15 -        [_browser addObserver: self forKeyPath: @"services" options: NSKeyValueObservingOptionNew context: NULL];
    2.16 -        [_browser addObserver: self forKeyPath: @"browsing" options: NSKeyValueObservingOptionNew context: NULL];
    2.17 +        [_browser addObserver: self forKeyPath: @"services" 
    2.18 +                      options: NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew 
    2.19 +                      context: NULL];
    2.20 +        [_browser addObserver: self forKeyPath: @"browsing" 
    2.21 +                      options: NSKeyValueObservingOptionNew
    2.22 +                      context: NULL];
    2.23          [_browser start];
    2.24          
    2.25          MYBonjourRegistration *myReg = _browser.myRegistration;
    2.26 @@ -243,7 +248,7 @@
    2.27  
    2.28  - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
    2.29  {
    2.30 -    LogTo(Bonjour,@"Observed change in %@: %@",keyPath,change);
    2.31 +    Log(@"Observed change in %@: %@",keyPath,change);
    2.32      if( $equal(keyPath,@"services") ) {
    2.33          if( [[change objectForKey: NSKeyValueChangeKindKey] intValue]==NSKeyValueChangeInsertion ) {
    2.34              NSSet *newServices = [change objectForKey: NSKeyValueChangeNewKey];
    2.35 @@ -252,8 +257,18 @@
    2.36                  Log(@"##### %@ : at %@:%hu, TXT=%@", 
    2.37                        service, hostname, service.port, service.txtRecord);
    2.38                  service.addressLookup.continuous = YES;
    2.39 +                [service.addressLookup addObserver: self
    2.40 +                                        forKeyPath: @"addresses"
    2.41 +                                           options: NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew
    2.42 +                                           context: NULL];
    2.43                  [service queryForRecord: kDNSServiceType_NULL];
    2.44              }
    2.45 +        } else if( [[change objectForKey: NSKeyValueChangeKindKey] intValue]==NSKeyValueChangeRemoval ) {
    2.46 +            NSSet *oldServices = [change objectForKey: NSKeyValueChangeOldKey];
    2.47 +            for( MYBonjourService *service in oldServices ) {
    2.48 +                Log(@"##### REMOVED: %@", service);
    2.49 +                [service.addressLookup removeObserver: self forKeyPath: @"addresses"];
    2.50 +            }
    2.51          }
    2.52      }
    2.53  }
     3.1 --- a/MYNetwork.xcodeproj/project.pbxproj	Tue May 05 22:19:33 2009 -0700
     3.2 +++ b/MYNetwork.xcodeproj/project.pbxproj	Sun May 10 19:00:50 2009 -0700
     3.3 @@ -351,23 +351,23 @@
     3.4  		270461220DE49055003D9D3F /* MYUtilities */ = {
     3.5  			isa = PBXGroup;
     3.6  			children = (
     3.7 +				270461880DE49634003D9D3F /* CollectionUtils.h */,
     3.8 +				270461870DE49634003D9D3F /* CollectionUtils.m */,
     3.9  				278C1BB50F9F975700954AE1 /* ConcurrentOperation.h */,
    3.10  				278C1BB60F9F975700954AE1 /* ConcurrentOperation.m */,
    3.11 -				270462C10DE4A64B003D9D3F /* MYUtilitiesTest_main.m */,
    3.12 -				270462C00DE4A639003D9D3F /* MYUtilities_Prefix.pch */,
    3.13 -				270461880DE49634003D9D3F /* CollectionUtils.h */,
    3.14 -				270461870DE49634003D9D3F /* CollectionUtils.m */,
    3.15  				270461360DE4918D003D9D3F /* ExceptionUtils.h */,
    3.16  				270461350DE4918D003D9D3F /* ExceptionUtils.m */,
    3.17 +				27E0DBEC0DF3450F00E7F648 /* GoogleToolboxSubset */,
    3.18  				2704612B0DE49088003D9D3F /* Logging.h */,
    3.19  				2704612A0DE49088003D9D3F /* Logging.m */,
    3.20 +				274122DD0F9CDD1600F21842 /* MYUtilities_Debug.xcconfig */,
    3.21 +				270462C00DE4A639003D9D3F /* MYUtilities_Prefix.pch */,
    3.22 +				274122DE0F9CDD1600F21842 /* MYUtilities_Release.xcconfig */,
    3.23 +				270462C10DE4A64B003D9D3F /* MYUtilitiesTest_main.m */,
    3.24  				270461450DE491A6003D9D3F /* Target.h */,
    3.25  				270461460DE491A6003D9D3F /* Target.m */,
    3.26  				270461290DE49088003D9D3F /* Test.h */,
    3.27  				270461280DE49088003D9D3F /* Test.m */,
    3.28 -				274122DD0F9CDD1600F21842 /* MYUtilities_Debug.xcconfig */,
    3.29 -				274122DE0F9CDD1600F21842 /* MYUtilities_Release.xcconfig */,
    3.30 -				27E0DBEC0DF3450F00E7F648 /* GoogleToolboxSubset */,
    3.31  			);
    3.32  			name = MYUtilities;
    3.33  			sourceTree = MYUtilities;