Skip to content

Instantly share code, notes, and snippets.

@ToeJamson
Created September 16, 2013 18:07
Show Gist options
  • Select an option

  • Save ToeJamson/6584281 to your computer and use it in GitHub Desktop.

Select an option

Save ToeJamson/6584281 to your computer and use it in GitHub Desktop.
Adding Key-Value Observing to PubNub
[sender addObserver: self
forKeyPath:key
options:NSKeyValueObservingOptionNew
context:NULL];
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"];
}
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]];
}
….
}
[[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]];
}
}
}];
[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