从线程调用委托方法

发布于 2024-10-15 03:29:13 字数 1124 浏览 3 评论 0原文

我有这段代码:

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

而且似乎只有使用委托我才会得到这个错误,而且我不知道如何解决。

提前致谢 :)

I've this bit of code:

- (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];   
}

it works nicely, but sometimes I get this error:

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.

And it seems that only using the delegate I get this error, and I don't know how to resolve.

Thanks in advance :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

许一世地老天荒 2024-10-22 03:29:13

我最近遇到了同样的问题。

可能有一些活动视图(例如UITextFieldUITextView)。在访问 delegate 之前尝试 resignFirstResponder 这些视图

I've run into the same problem recently.

There may be some active views (eg. UITextField,UITextView). Try resignFirstResponder those views before accessing delegate

柠北森屋 2024-10-22 03:29:13

您可以通过非常仔细地考虑应用程序的并发架构并确保您没有从线程中执行任何只应在主线程上完成的操作来解决该问题。

在这种情况下,您将导致 UIKit 从辅助线程执行代码。如果您要在 _WebThreadLockFromAnyThread 上设置断点,您就会确切地知道在哪里。

除了最受严格控制的情况之外,从辅助线程使用应用程序的委托是非常不典型的。

tl;dr 您无法通过针对随机选择器分离新线程来使应用程序线程化。

You fix the problem by very carefully thinking through your application's concurrency architecture and ensuring that you aren't exercising anything from a thread that should only be done on the main thread.

In this case, you are causing the UIKit to execute code from a secondary thread. If you were to set a breakpoint on _WebThreadLockFromAnyThread, you would know exactly where.

It is exceedingly atypical to use the app's delegate from a secondary thread in anything but the most extremely controlled circumstances.

tl;dr You can't make an app threaded by detaching a new thread against a random selector.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文