Android
In Android, if we want to wait for the screen to refresh we can send a delayed message to the main thread handler as in:
sendMessageDelayed(obtainMessage(0),200);
We can then repopulate the screen after a delay of 200 ms in the main thread handler, switching on what 0 as in:
private Handler myHandler= new Handler(){
@Override
public void handleMessage(Message msg){
switch(msg.what){
case 0:
// UPDATE WIDGETS HERE
break;
default:
super.handleMessage(msg);
break;
}
}
};
Or we can add a delayed runnable to the message queue using postDelayed defined as:
public final boolean postDelayed (Runnable r, long delayMillis)
iOS
In iOS, we can post a delayed message to the main message queue using dispatch_after and blocks as in:
-(IBAction) encrypt:(id)sender {
cipherText.text=@""; // too fast to see
errorText.text=@"";
double delayInSeconds = .2;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[self tryEncrypt:plainText.text password:password.text salt:nil];
});
}
-(void)tryEncrypt:(NSString*)inText password:(NSString*)pw salt:(NSData*)saltValue {
@try {
cipherText.text=[model encrypt:inText
password:pw
salt:saltValue];
}
@catch (NSException *e) {
errorText.text= [e description];
NSLog(@"%@",[e description]);
}
}
In this case, the text views are first cleared. After a delay of .2 seconds, the text views are repopulated.
According to the Apple docs "Blocks are particular useful as a callback because the block carries both the code to be executed on callback and the data needed during that execution." In this example, we are not doing a callback, but we are packaging the code and the data to be "called" after a delay on the GUI message queue.
Or you can use selector as in:
[lError performSelector:@selector(setText:) withObject:@"" afterDelay:0.5];
Despite my attempts, I was unable to get Selectors to work with multiple parameters, so I am sticking to dispatch_after for now. Besides, NSTimer is implemented using such lower level calls.