Skip to content

Instantly share code, notes, and snippets.

@AlexanderWingard
Created December 7, 2012 10:40
Show Gist options
  • Save AlexanderWingard/4232411 to your computer and use it in GitHub Desktop.
Save AlexanderWingard/4232411 to your computer and use it in GitHub Desktop.
Updating view with slow and unreliable data
-(void)updateTextLabel {
[self fetchTextAndThen:^(NSString *result){
self.textLabel.text = result;
}];
}
-(void)fetchTextAndThen:(void (^)(NSString *result))block {
NSString *current = [[NSUserDefaults standardUserDefaults] objectForKey:@"Text"];
dispatch_async(webQueue, ^{
NSString *response = [self requestToURL:@"http://192.168.0.108:8888/getText" withData:@"" withMethod:@"GET"];
dispatch_async(mainQueue, ^{
if (![response isEqualToString:current] && response != nil) {
[[NSUserDefaults standardUserDefaults] setObject:response forKey:@"Text"];
[[NSUserDefaults standardUserDefaults] synchronize];
block(response);
}
});
});
block(current);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment