UITextField access from outside the main thread
UITextField access from outside the main thread
- Subject: UITextField access from outside the main thread
- From: Aaron Montgomery <email@hidden>
- Date: Thu, 05 Sep 2013 09:23:43 -0700
Hi,
I'm currently running into this issue trying to use Telerik Test Studio (and am asking about it there as well), but I think the issue is more general and would like some ideas.
The situation: Test Studio drives my app's user interface from a background thread. In some places where the Test Studio is just interacting wth UI elements, I get a WebThreadLockFromAnyThread warnings (which is expected since you shouldn't interact with the UI from another thread). I believe they've done the coding to avoid the warning in most cases, but I've got some subclasses of UITextField that are generating the warnings. Note, in all examples, self is an object whose class is a subclass of UITextField.
In some places, the solution is to use blocks, replacing:
[self.objectField reloadInputViews];
with:
dispatch_async(dispatch_get_main_queue(), ^{ [self.objectField reloadInputViews]; });
which works. The problem is places where I want to read the data. In those cases, replacing:
NSString* theName = self.text;
with:
__block NSString* theName = nil;
dispatch_sync(dispatch_get_main_queue(), ^{ theName = self.text; });
will block the main thread if run from the main thread (note that I really need a dispatch_sync so I can get the return value before continuing). So I end up with the replacement:
__block NSString* theName = nil;
if ([NSThread isMainThread])
{
theName = self.text;
}
else
{
dispatch_sync(dispatch_get_main_queue(), ^{ theName = self.text; });
}
which is now looking like an awful lot of work just to read a data value and adding a lot of extra code just to accommodate Test Studio.
So the questions:
Are there any pitfalls to the replacements above that I'm missing?
Should I be doing this in any case to make my code safer?
Is there a better way to go about accessing self.text that works on any thread?
If I wanted to actually update self.text from any thread (haven't needed to do this yet, but it may arise) would the code below be my best option?
if ([NSThread isMainThread])
{
self.text = theName;
}
else
{
dispatch_sync(dispatch_get_main_queue(), ^{ self.text = theName; });
}
Thanks,
Aaron
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden