我有這段代碼:委託方法
- (IBAction)registerAction:(id)sender {
[NSThread detachNewThreadSelector:@selector(registerThread) toTarget:self withObject:nil];
}
- (void)registerThread {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
MyDelegate *delegate = (MyDelegate *)[[UIApplication sharedApplication] delegate];
NSInteger locationID = [delegate.api checkCoordinate:[NSString stringWithFormat:@"%f,%f",
location.coordinate.latitude, location.coordinate.longitude]];
NSNumber *status = [api registerWithUsername:usernameField.text
password:passwordField.text email:emailField.text andLocation:locationID];
[self performSelectorOnMainThread:@selector(registrationDoneWithStatus:) withObject:[NSNumber numberWithInt:1] waitUntilDone:NO];
[pool release];
}
它工作得很好,但有時我得到這個錯誤:
void _WebThreadLockFromAnyThread(bool), 0x6157e30: 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.
而且似乎只有使用委託我得到這個錯誤,我不知道如何解決。
感謝提前:)
你是什麼意思:「你不能通過將一個新線程與一個隨機選擇器分開來使應用線程化。」? – patrick 2011-01-28 23:20:15
您不能採用任何隨機方法,並通過`detachNewThreadSelector:`在線程中執行該方法,並且希望發生正確的事情。不是說這就是你在這裏做的 - 只是一個普遍的規則。線程很難。 – bbum 2011-01-29 00:26:41