0

我有一个我喜欢这样的segue:

[self performSegueWithIdentifier:@"PlanToBusiness" sender:self];

它适用于 iPhone,但不适用于 iPad。我所做的是对服务器进行远程调用,当服务器响应正确返回时,我尝试使用该 segue 将用户带到新页面。

它适用于 iPhone 模拟器,但不适用于 iPad(两者的标识字符串相同),在 iPad 上它会因以下错误而崩溃:

bool _WebTryThreadLock(bool), 0x68f9cb0: 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   WebThreadLock
2   -[UITextRangeImpl isEmpty]
3   -[UITextRange(UITextSelectionAdditions) _isCaret]
4   -[UITextSelectionView setCaretBlinks:]
5   -[UIKeyboardImpl setCaretBlinks:]
6   -[UIKeyboardImpl setDelegate:force:]
7   -[UIKeyboardImpl setDelegate:]
8   -[UIPeripheralHost(UIKitInternal) _reloadInputViewsForResponder:]
9   -[UINavigationController navigationTransitionView:didStartTransition:]
10  -[UINavigationTransitionView transition:fromView:toView:]
11  -[UINavigationTransitionView transition:toView:]
12  -[UINavigationController _startTransition:fromViewController:toViewController:]
13  -[UINavigationController _startDeferredTransitionIfNeeded]
14  -[UINavigationController pushViewController:transition:forceImmediate:]
15  -[UINavigationController pushViewController:animated:]
16  -[UIStoryboardPushSegue perform]
17  -[UIStoryboardSegueTemplate perform:]
18  -[UIViewController performSegueWithIdentifier:sender:]
19  __41-[PlanBusinessController submitBusiness:]_block_invoke_0
20  __block_global_0
21  -[NSBlockOperation main]
22  -[__NSOperationInternal start]
23  -[NSOperation start]
24  __block_global_6
25  _dispatch_call_block_and_release
26  _dispatch_worker_thread2
27  _pthread_wqthread
28  start_wqthread

我也不太习惯阅读 Obective-C 堆栈跟踪,所以我很难确定问题所在。

这是我如何做到的:

- (IBAction)submitBusiness:(id)sender 
{    
     // Make a url to send

     NSURL *url = [NSURL URLWithString:full_encoded_url_string];
     NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];

        // ***************
        // TODO: ok I dont really understand what this is
        NSOperationQueue *queue = [[NSOperationQueue alloc] init];
        // **************

        [NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
         {   
                         // I took out a lot of the logic code that is not needed

                         [self performSegueWithIdentifier:@"PlanToBusiness" sender:self];
         }];
    }
}
4

1 回答 1

2

当方法接触 UI 时,例如执行 segue 时,确保从主线程调用该代码很重要。最简单的方法是制作这样的方法:

- (void)performPlanToBusinessSegueOnMainThread
{
    [self performSegueWithIdentifier:@"PlanToBusiness" sender:self];
}

然后,您可以在完成的块中调用此方法,如下所示:

[NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
 {   
     // I took out a lot of the logic code that is not needed

     [self performSelectorOnMainThread:@selector(performPlanToBusinessSegueOnMainThread) 
                            withObject:nil 
                         waitUntilDone:YES];
 }];

这有点迂回,但它应该可以解决您的异常。如果我们可以只调用performSegueWithIdentifierperformSelectorOnMainThread方法会更好,但是它的参数太多了。

于 2012-08-17T17:02:38.910 回答