0

我在我的项目中使用 MBPogressHUD,并且我已经正确设置了所有库、导入等。但是,我遇到了一个似乎与选择器方法有关的崩溃。我有一个名为“getInfo”的方法,它基本上连接到服务器。HUD 通过按下按钮触发。在对崩溃进行了一些研究之后,人们说将初始化放在 viewWillAppear 中,因为一些初始化时间占用了实际执行任务和显示 HUD 所需的时间。

    -(void)viewWillAppear:(BOOL)animated {
    connectionHUD = [[MBProgressHUD alloc]initWithView:self.view];
    [self.view addSubview:connectionHUD];
    connectionHUD.labelText = @"Connecting";
    connectionHUD.mode = MBProgressHUDModeAnnularDeterminate;    
}

-(IBAction)connectButton:(id)sender {


    [connectionHUD showWhileExecuting:@selector(getInfo) onTarget:self withObject:nil animated:YES];
}

碰撞:

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



-(void)getInfo {

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
UserPi *newPi = [[UserPi alloc]init];
newPi.passWord = self.passTextField.text;
[defaults setObject:self.passTextField.text forKey:@"password"];
newPi.userName = self.userTextField.text;
[defaults setObject:self.userTextField.text forKey:@"username"];
newPi.ipAddress = self.ipTextField.text;
[defaults setObject:self.ipTextField.text forKey:@"ip"];
[newPi connectToServer];
NSString* newAddress = [newPi returnIP];
self.connected = newPi.connected;
[self.delegate sendIP:newAddress];
[self.delegate isConnected:self.connected];
[defaults synchronize];
[self.navigationController popToRootViewControllerAnimated:YES];


}

完整错误:

bool _WebTryThreadLock(bool), 0x7327740: 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...
1   0x3a1ffe9 WebThreadLock
2   0x4ec8ff -[UITextRangeImpl isEmpty]
3   0x4ec4db -[UITextRange(UITextInputAdditions) _isCaret]
4   0x48e7b6 -[UITextSelectionView setCaretBlinks:]
5   0x328f79 -[UIKeyboardImpl setCaretBlinks:]
6   0x3185bc -[UIKeyboardImpl setDelegate:force:]
7   0x3184ae -[UIKeyboardImpl setDelegate:]
8   0x53ff65 -[UIPeripheralHost(UIKitInternal) _reloadInputViewsForResponder:]
9   0x29215b -[UINavigationController navigationTransitionView:didStartTransition:]
10  0x418961 -[UINavigationTransitionView transition:fromView:toView:]
11  0x418658 -[UINavigationTransitionView transition:toView:]
12  0x294651 -[UINavigationController _startTransition:fromViewController:toViewController:]
13  0x29489b -[UINavigationController _startDeferredTransitionIfNeeded:]
14  0x295dc6 _popViewControllerNormal
15  0x296065 -[UINavigationController _popViewControllerWithTransition:allowPoppingLast:]
16  0xe6124b0 -[UINavigationControllerAccessibility(SafeCategory) _popViewControllerWithTransition:allowPoppingLast:]
17  0x2961a8 -[UINavigationController popViewControllerWithTransition:]
18  0x2965b9 -[UINavigationController popToViewController:transition:]
19  0x296257 -[UINavigationController popToRootViewControllerWithTransition:]
20  0x2961de -[UINavigationController popToRootViewControllerAnimated:]
21  0x4868 -[ConnectionViewController getInfo]
22  0x126a6b0 -[NSObject performSelector:withObject:]
23  0x8657 -[MBProgressHUD launchExecution]
24  0xca1805 -[NSThread main]
25  0xca1764 __NSThread__main__
26  0x95108ed9 _pthread_start
27  0x9510c6de thread_start
4

3 回答 3

1

大多数 UIKit 框架方法都不是线程安全的,必须始终在主线程上调用。在您的情况下,getInfo特别是从后台线程调用 UIKit API,-[UINavigationController popToRootViewControllerAnimated:]

MBProgressHUD使您getInfo在后台线程上被调用。见方法showWhileExecuting:onTarget:withObject:animated:

尝试使用 GCD 在主线程上调度 UIKit 方法:

-(void)getInfo {    
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    UserPi *newPi = [[UserPi alloc]init];
    newPi.passWord = self.passTextField.text;
    [defaults setObject:self.passTextField.text forKey:@"password"];
    newPi.userName = self.userTextField.text;
    [defaults setObject:self.userTextField.text forKey:@"username"];
    newPi.ipAddress = self.ipTextField.text;
    [defaults setObject:self.ipTextField.text forKey:@"ip"];
    [newPi connectToServer];
    NSString* newAddress = [newPi returnIP];
    self.connected = newPi.connected;     
    // **NOTE**   
    // PS: self.delegate methods should not call UIKit methods
    // if they do, then move them into the main thread callback block
    [self.delegate sendIP:newAddress];
    [self.delegate isConnected:self.connected];
    [defaults synchronize];

    // Do UI work on main thread.
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.navigationController popToRootViewControllerAnimated:YES];    
    });
}
于 2013-06-04T01:25:01.297 回答
1

你是在调用-getInfo主线程吗?[self.navigationController popToRootViewControllerAnimated:YES];必须在主线程上调用。

于 2013-06-04T01:25:18.273 回答
0

问题是您的getInfo方法调用了 UI,如下所示:

[self.navigationController popToRootViewControllerAnimated:YES];

…但你在后台线程上运行它。

关键-showWhileExecuting: onTarget: withObject: animated:在于它会自动在后台线程中运行您的代码。

因此,您需要像在后台线程中手动运行时一样保护事物。

所以,你方法中的任何 UI 代码都需要使用performSelectorOnMainThread:和好友,dispatch_async或者其他方式做同样的事情。

在这种特殊情况下,您希望调度一个采用原始 ( BOOL) 参数的方法,这意味着您不能只使用-performSelectorOnMainThread: withObject:. 但是您可能还想等到它完成,这意味着您不能只使用dispatch_async.

您可以只编写一个不带参数的包装器方法:

- (void)popNavigationControllerToRoot {
    [self.navigationController popToRootViewControllerAnimated:YES];
}

… 接着:

[self performSelectorOnMainThread:@selector(popNavigationControllerToRoot)
                    waitUntilDone:YES];

或者您可以使用包装器NSInvocation,例如这里的包装器:

[[self.navigationController dd_invokeOnMainThreadAndWaitUntilDone:YES] 
 popToRootViewControllerAnimated:YES];
于 2013-06-04T01:24:32.707 回答