2012-08-17 48 views
0

我有一个赛格瑞,我这样做:错误模拟器为iPad而不是iPhone

[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 

我也不太习惯阅读O -ctive-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]; 
     }]; 
    } 
} 
+0

你如何处理远程调用服务器?如果它是一个异步调用,并且您尝试在调用Web服务而不是主线程的线程中继续执行,则可能导致此问题。你可能会发布更多的代码,请求消失的地方以及执行segue的全部函数? – 2012-08-17 16:51:39

+0

@Paul是的,我正在做一个异步调用,并尝试从返回代码继承。什么是正确的方式来做到这一点,以避免这种错误发生? ...现在发布代码... – GeekedOut 2012-08-17 16:53:26

+0

@Paul我只是添加了一些我使用的代码...我拿出了很多错误检查和其他逻辑,但我做的是在那里。 – GeekedOut 2012-08-17 17:00:24

回答

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

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

+0

聪明!我明白你在做什么。所以我应该把该函数的调用放在从服务器返回的代码的最后,对吧?这样我确保这个线程停止运行? – GeekedOut 2012-08-17 17:05:22

+0

只要你将'waituUntilDone'参数设置为'Yes',你就可以将它放在你想要的任何地方,它将运行得几乎相同,就好像它不是从一个不同的线程被调用一样(Except因为它希望不会抛出异常!)它只是让web线程等待,直到performSelectorOnMainThread函数返回到下一行。 – 2012-08-17 17:07:35

+0

谢谢! :) 非常感激。 – GeekedOut 2012-08-17 17:09:02

相关问题