-
-
Save ToeJamson/6584281 to your computer and use it in GitHub Desktop.
Adding Key-Value Observing to PubNub
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [sender addObserver: self | |
| forKeyPath:key | |
| options:NSKeyValueObservingOptionNew | |
| context:NULL]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| a = [[AutoUpdateImplementation alloc] init]; | |
| keyChannel = [NSString stringWithString: channel]; | |
| if(i == 0) { | |
| [a subscribeToChannelWithUpdates:sender | |
| onChannel:@"Ajan" | |
| forKeys:keys withErrorCallback:f]; | |
| i++; | |
| } | |
| else { | |
| [AutoUpdateErrorHandling printWarningForFunction:@"subscribeToChannelWithUpdates" forFile:@"PubNubKeyUpdate" withMessage:@"Repeated attempts to subscribe not allowed"]; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| printFunction = f; | |
| serverChannel = [PNChannel channelWithName:channel shouldObservePresence:YES]; | |
| [PubNub subscribeOnChannel:serverChannel]; | |
| keysToBeUpdated = [[NSArray alloc] initWithArray: keys]; | |
| sourceOfKeys = sender; | |
| for(id key in keysToBeUpdated) { | |
| if ([key rangeOfString:@"text"].location == NSNotFound) { | |
| [AutoUpdateErrorHandling printWarningForFunction:@"subscribeToChannelWithUpdates" forFile:@"AutoUpdateImplementation" withMessage:[@" .text not found in key " stringByAppendingString: key]]; | |
| } | |
| …. | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [[PNObservationCenter defaultCenter] addMessageReceiveObserver:self | |
| withBlock:^(PNMessage *message) { | |
| NSDictionary *messageDict = message.message; | |
| if([[messageDict objectForKey: @"type"] isEqualToString: @"update"]) { | |
| NSString *key = [messageDict objectForKey: @"key"]; | |
| NSString *value = [messageDict objectForKey: @"value"]; | |
| [self unregisterForChangeNotification:key sender:sender]; | |
| @try { | |
| [sender setValue:value forKeyPath:key]; | |
| } | |
| @catch(NSException *e) { | |
| printFunction(); | |
| [AutoUpdateErrorHandling printErrorForFunction:@"subscribeToChannelWithUpdates" forFile:@"AutoUpdateImplementation" withMessage:[@"Key sent to application is not key-value coding compliant./nException name: " stringByAppendingString: e.name]]; | |
| } | |
| @try { | |
| [sender addObserver: self | |
| forKeyPath:key | |
| options:NSKeyValueObservingOptionNew | |
| context:NULL]; | |
| } | |
| @catch(NSException *e) { | |
| printFunction(); | |
| [AutoUpdateErrorHandling printErrorForFunction:@"subscribeToChannelWithUpdates" forFile:@"AutoUpdateImplementation" withMessage:[@"Add observer received an unknown key./nException name: " stringByAppendingString: e.name]]; | |
| } | |
| } | |
| }]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [PubNubKeyUpdate subscribeToChannelWithUpdates:self | |
| onChannel:@"Ajan" | |
| forKeys:[[NSArray alloc] initWithObjects: | |
| @"balanceLabel.text", nil]]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment