Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
287 views
in Technique[技术] by (71.8m points)

iphone - Warning: UIKit should not be called from a secondary thread

In my iPhone app I need to connect to a web server as this can take some time I'm using threads like this:

[NSThread detachNewThreadSelector:@selector(sendStuff) toTarget:self withObject:nil];

- (void)sendStuff {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    //Need to get the string from the textField to send to server
    NSString *myString = self.textField.text;

    //Do some stuff here, connect to web server etc..

    [pool release];
}

On the row where I use self.textField I get a warning in console saying: void _WebThreadLockFromAnyThread(bool), 0x5d306b0: Obtaining the web lock from a thread other than the main thread or the web thread. UIKit should not be called from a secondary thread.

How can I use the textField without getting this error?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It depends a little bit on what you want to do with the textField. If reading the value is the only thing, you can do:

[NSThread detachNewThreadSelector:@selector(sendStuff) toTarget:self withObject:self.textField.text];

- (void)sendStuff:(NSString*)myString {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    //Do someting with myString
    [pool release];
}

If you want to change a value on the textField you could:

[self.textField performSelectorOnMainThread:@selector(setText:) withObject:@"new Text"];

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...