iPad模拟器出错,但iPhone出现错误

时间:2012-08-17 16:30:45

标签: ios ios5 uistoryboardsegue

我有一个我喜欢这个的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];
         }];
    }
}

1 个答案:

答案 0 :(得分: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];
 }];

这有点迂回,但它应该修复你的异常。如果我们可以在performSegueWithIdentifier方法中调用performSelectorOnMainThread会更好,但它的参数太多了。