Show
Ignore:
Timestamp:
16-04-2008 18:01:32 (5 years ago)
Author:
jppavao
Message:

Improved KVO registration to members of collections in some places in order to avoid using loops.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/lilypad/Sources/LPRosterController.m

    r267 r270  
    246246- (void)p_startObservingAccounts:(NSArray *)accounts 
    247247{ 
     248        NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, [accounts count])]; 
     249         
     250        [accounts addObserver:self toObjectsAtIndexes:indexSet 
     251                           forKeyPath:@"enabled" 
     252                                  options:0 context:LPAccountIDChangeContext]; 
     253        [accounts addObserver:self toObjectsAtIndexes:indexSet 
     254                           forKeyPath:@"name" 
     255                                  options:0 context:LPAccountIDChangeContext]; 
     256        [accounts addObserver:self toObjectsAtIndexes:indexSet 
     257                           forKeyPath:@"JID" 
     258                                  options:0 context:LPAccountIDChangeContext]; 
     259        [accounts addObserver:self toObjectsAtIndexes:indexSet 
     260                           forKeyPath:@"pubManager.mainPubURL" 
     261                                  options:0 context:LPPubChangeContext]; 
     262        [accounts addObserver:self toObjectsAtIndexes:indexSet 
     263                           forKeyPath:@"pubManager.statusPhraseHTML" 
     264                                  options:0 context:LPPubChangeContext]; 
     265         
    248266        NSEnumerator *accountsEnum = [accounts objectEnumerator]; 
    249267        LPAccount *account; 
    250          
    251268        while (account = [accountsEnum nextObject]) { 
    252                 [account addObserver:self forKeyPath:@"enabled" options:0 context:LPAccountIDChangeContext]; 
    253                 [account addObserver:self forKeyPath:@"name" options:0 context:LPAccountIDChangeContext]; 
    254                 [account addObserver:self forKeyPath:@"JID" options:0 context:LPAccountIDChangeContext]; 
    255                 [account addObserver:self forKeyPath:@"pubManager.mainPubURL" options:0 context:LPPubChangeContext]; 
    256                 [account addObserver:self forKeyPath:@"pubManager.statusPhraseHTML" options:0 context:LPPubChangeContext]; 
    257                  
    258269                [[NSNotificationCenter defaultCenter] addObserver:self 
    259270                                                                                                 selector:@selector(accountWillChangeStatus:) 
     
    265276- (void)p_stopObservingAccounts:(NSArray *)accounts 
    266277{ 
     278        NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, [accounts count])]; 
     279         
     280        [accounts removeObserver:self fromObjectsAtIndexes:indexSet forKeyPath:@"pubManager.statusPhraseHTML"]; 
     281        [accounts removeObserver:self fromObjectsAtIndexes:indexSet forKeyPath:@"pubManager.mainPubURL"]; 
     282        [accounts removeObserver:self fromObjectsAtIndexes:indexSet forKeyPath:@"JID"]; 
     283        [accounts removeObserver:self fromObjectsAtIndexes:indexSet forKeyPath:@"name"]; 
     284        [accounts removeObserver:self fromObjectsAtIndexes:indexSet forKeyPath:@"enabled"]; 
     285         
    267286        NSEnumerator *accountsEnum = [accounts objectEnumerator]; 
    268287        LPAccount *account; 
    269          
    270288        while (account = [accountsEnum nextObject]) { 
    271289                [[NSNotificationCenter defaultCenter] removeObserver:self name:LPAccountWillChangeStatusNotification object:account]; 
    272                  
    273                 [account removeObserver:self forKeyPath:@"pubManager.statusPhraseHTML"]; 
    274                 [account removeObserver:self forKeyPath:@"pubManager.mainPubURL"]; 
    275                 [account removeObserver:self forKeyPath:@"JID"]; 
    276                 [account removeObserver:self forKeyPath:@"name"]; 
    277                 [account removeObserver:self forKeyPath:@"enabled"]; 
    278290        } 
    279291} 
     
    281293- (void)p_startObservingGroups:(NSArray *)groups 
    282294{ 
    283         NSEnumerator *groupEnumerator = [groups objectEnumerator]; 
    284         id group; 
    285         while (group = [groupEnumerator nextObject]) { 
    286                 [group addObserver:self 
    287                                 forKeyPath:@"name" 
    288                                    options:( NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld ) 
    289                                    context:LPRosterGroupPropertyChangeContext]; 
    290                 [group addObserver:self 
    291                                 forKeyPath:@"type" 
    292                                    options:( NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld ) 
    293                                    context:LPRosterGroupPropertyChangeContext]; 
    294                 [group addObserver:self 
    295                                 forKeyPath:@"contacts" 
    296                                    options:( NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld ) 
    297                                    context:LPRosterCollectionsChangeContext]; 
    298         } 
     295        NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, [groups count])]; 
     296         
     297        [groups addObserver:self toObjectsAtIndexes:indexSet 
     298                         forKeyPath:@"name" 
     299                                options:( NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld ) 
     300                                context:LPRosterGroupPropertyChangeContext]; 
     301        [groups addObserver:self toObjectsAtIndexes:indexSet 
     302                         forKeyPath:@"type" 
     303                                options:( NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld ) 
     304                                context:LPRosterGroupPropertyChangeContext]; 
     305        [groups addObserver:self toObjectsAtIndexes:indexSet 
     306                         forKeyPath:@"contacts" 
     307                                options:( NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld ) 
     308                                context:LPRosterCollectionsChangeContext]; 
    299309} 
    300310 
     
    302312- (void)p_stopObservingGroups:(NSArray *)groups 
    303313{ 
    304         NSEnumerator *groupEnumerator = [groups objectEnumerator]; 
    305         id group; 
    306         while (group = [groupEnumerator nextObject]) { 
    307                 [group removeObserver:self forKeyPath:@"name"]; 
    308                 [group removeObserver:self forKeyPath:@"type"]; 
    309                 [group removeObserver:self forKeyPath:@"contacts"]; 
    310         } 
     314        NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, [groups count])]; 
     315         
     316        [groups removeObserver:self fromObjectsAtIndexes:indexSet forKeyPath:@"name"]; 
     317        [groups removeObserver:self fromObjectsAtIndexes:indexSet forKeyPath:@"type"]; 
     318        [groups removeObserver:self fromObjectsAtIndexes:indexSet forKeyPath:@"contacts"]; 
    311319} 
    312320 
     
    314322- (void)p_startObservingContacts:(NSArray *)contacts 
    315323{ 
    316         NSEnumerator *contactEnumerator = [contacts objectEnumerator]; 
    317         id contact; 
    318         while (contact = [contactEnumerator nextObject]) { 
    319                 [contact addObserver:self forKeyPath:@"name" options:0 context:LPRosterItemPropertyChangeContext]; 
    320                 [contact addObserver:self forKeyPath:@"avatar" options:0 context:LPRosterItemPropertyChangeContext]; 
    321                 [contact addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionOld context:LPRosterItemPropertyChangeContext]; 
    322                 [contact addObserver:self forKeyPath:@"statusMessage" options:0 context:LPRosterItemPropertyChangeContext]; 
    323         } 
     324        NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, [contacts count])]; 
     325         
     326        [contacts addObserver:self toObjectsAtIndexes:indexSet 
     327                           forKeyPath:@"name" 
     328                                  options:0 context:LPRosterItemPropertyChangeContext]; 
     329        [contacts addObserver:self toObjectsAtIndexes:indexSet 
     330                           forKeyPath:@"avatar" 
     331                                  options:0 context:LPRosterItemPropertyChangeContext]; 
     332        [contacts addObserver:self toObjectsAtIndexes:indexSet 
     333                           forKeyPath:@"status" 
     334                                  options:NSKeyValueObservingOptionOld context:LPRosterItemPropertyChangeContext]; 
     335        [contacts addObserver:self toObjectsAtIndexes:indexSet 
     336                           forKeyPath:@"statusMessage" 
     337                                  options:0 context:LPRosterItemPropertyChangeContext]; 
    324338} 
    325339 
     
    327341- (void)p_stopObservingContacts:(NSArray *)contacts 
    328342{ 
    329         NSEnumerator *contactEnumerator = [contacts objectEnumerator]; 
    330         id contact; 
    331         while (contact = [contactEnumerator nextObject]) { 
    332                 [contact removeObserver:self forKeyPath:@"name"]; 
    333                 [contact removeObserver:self forKeyPath:@"avatar"]; 
    334                 [contact removeObserver:self forKeyPath:@"status"]; 
    335                 [contact removeObserver:self forKeyPath:@"statusMessage"]; 
    336         } 
     343        NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, [contacts count])]; 
     344         
     345        [contacts removeObserver:self fromObjectsAtIndexes:indexSet forKeyPath:@"name"]; 
     346        [contacts removeObserver:self fromObjectsAtIndexes:indexSet forKeyPath:@"avatar"]; 
     347        [contacts removeObserver:self fromObjectsAtIndexes:indexSet forKeyPath:@"status"]; 
     348        [contacts removeObserver:self fromObjectsAtIndexes:indexSet forKeyPath:@"statusMessage"]; 
    337349} 
    338350