1

私はこのビットのコードを持っています:

- (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.

そして、デリゲートのみを使用するとこのエラーが発生するようで、解決方法がわかりません。

前もって感謝します :)

4

2 に答える 2

3

私は最近同じ問題に遭遇しました。

いくつかのアクティブなビューがある場合があります (例: UITextFieldUITextView)。resignFirstResponderアクセスする前にそれらのビューを試してくださいdelegate

于 2011-05-05T11:10:56.347 に答える
1

アプリケーションの同時実行アーキテクチャを慎重に検討し、メイン スレッドでのみ実行する必要があるスレッドから何も実行していないことを確認することで、問題を解決します。

この場合、UIKit にセカンダリ スレッドからコードを実行させています。_WebThreadLockFromAnyThread にブレークポイントを設定すると、正確な場所がわかります。

最も厳密に制御された状況以外で、セカンダリ スレッドからアプリのデリゲートを使用することは非常に一般的ではありません。

tl;dr ランダム セレクターに対して新しいスレッドをデタッチしてアプリをスレッド化することはできません。

于 2011-01-28T22:59:07.203 に答える