Created
December 7, 2012 10:40
-
-
Save AlexanderWingard/4232411 to your computer and use it in GitHub Desktop.
Updating view with slow and unreliable data
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
-(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