Spooky xcode thread error

Upgraded to Xcode 3.1.3 pre-release and suddenly my iPhone app didn’t build!

I recieved this error:
bool _WebTryThreadLock(bool), 0xe755f0: Tried to obtain the web lock from a thread other than the main thread or the web thread. This may be a result of calling to UIKit from a secondary thread. Crashing now…

As you might know (or understand by this post) I am pretty new to Objective-C. Anyhow after some twists and turns I found out that if I pull the textMessage.text in sendMessage method it works. Why is this?

– (void) sendMessage {
// Override point for customization after application launch
[progressIndicator setHidden:NO];
[progressIndicator startAnimating];
[buttonSend setHidden:YES];
NSLog(@”Load message from UI: %@.”, textMessage.text);
textMessage.text = textMessage.text; // Spooky. If I remove this line I crash! Why?
[NSThread detachNewThreadSelector: @selector(popAway) toTarget:self withObject:nil];
}
– (void) popAway {
NSLog(@”Send started.”);
NSAutoreleasePool *pool = [ [ NSAutoreleasePool alloc ] init ];
NSLog(@”Load message from UI.”);
NSString *message = [ textMessage.text stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding ];
NSLog(@”Load mobile preference.”);
[pool release];
}
5 comments
  1. You’re trying to interact with UI elements on a background thread. That’s a no-no

    1. Thanks!
      Will do a redesign and send the element data in the method call instead of calling them directly from the thread.
      Hope that will solve the issue.

    2. Ok, I removed the thread stuff. The only reason to use it was to get a working progress bar.
      I need to understand how to pass parameters with the detachNewThreadSelector: @selector method before I add it again.

      Thanks for the hint!

  2. You may as well use NSOperationQueue with NSInvocationOperation class, that’s how i managed anyway.

  3. Try this:
    [self performSelectorOnMainThread:@selector(popAway) withObject:nil waitUntilDone:NO];

Comments are closed.