Last active
November 16, 2015 09:28
Challenge 1
This file contains 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
@interface XYZWaitController : UIViewController | |
@property (strong, nonatomic) UILabel *alert; | |
@end | |
@implementation XYZWaitController | |
- (void)viewDidLoad | |
{ | |
CGRect frame = CGRectMake(20, 200, 200, 20); | |
self.alert = [[UILabel alloc] initWithFrame:frame]; | |
self.alert.text = @"Please wait 10 seconds..."; | |
self.alert.textColor = [UIColor whiteColor]; | |
[self.view addSubview:self.alert]; | |
NSOperationQueue *waitQueue = [[NSOperationQueue alloc] init]; | |
[waitQueue addOperationWithBlock:^{ | |
[NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:10]]; | |
self.alert.text = @"Thanks!"; | |
}]; | |
} | |
@end | |
@implementation XYZAppDelegate | |
- (BOOL)application:(UIApplication *)application | |
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions | |
{ | |
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; | |
self.window.rootViewController = [[XYZWaitController alloc] init]; | |
[self.window makeKeyAndVisible]; | |
return YES; | |
} |
The problem is that ui elements must changed in main queue then you can write:
NSOperationQueue *waitQueue = [NSOperationQueue mainQueue];
[waitQueue addOperationWithBlock:^{
[NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:10]];
self.alert.text = @"Thanks!";
}];
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
// The problem here:
NSOperationQueue *waitQueue = [[NSOperationQueue alloc] init];
[waitQueue addOperationWithBlock:^{
[NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:10]];
self.alert.text = @"Thanks!";
}];
// You should access the UI throw main thread like this to grantee the block will execute on the main thread:
NSOperationQueue *waitQueue = [[NSOperationQueue alloc] init];
[waitQueue addOperationWithBlock:^{
[NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:10]];
[[NSOperationQueu mainQueue] addOperationWithBlock:^{
self.alert.text = @"Thanks!";
}];
}];