I am doing a complex application that has to do stuff on a lot of threads and frequently update the interface.
So I have to add a lot of
dispatch_async(dispatch_get_main_queue(), ^{
});
in the middle of the code to dispatch UI updates to the main thread and I find it ugly and disruptive as hell.
So, I have this idea of creating subclasses of elements like UILabel
, UITextField
, etc., overriding their main thread methods like this:
- (void)setAttributedText:(NSAttributedString *)attributedText {
dispatch_async(dispatch_get_main_queue(), ^{
[super setAttributedText:attributedText];
});
}
- (void)setText:(NSString *)text {
dispatch_async(dispatch_get_main_queue(), ^{
[super setText:text];
});
}
- (void)scrollRangeToVisible:(NSRange)range {
dispatch_async(dispatch_get_main_queue(), ^{
[super scrollRangeToVisible:range];
});
}
but again, I have to have this same code scatter all over these classes.
Is there a better way?