performSegueWithIdentifier 需要超过一分钟才能执行或崩溃

Posted

技术标签:

【中文标题】performSegueWithIdentifier 需要超过一分钟才能执行或崩溃【英文标题】:performSegueWithIdentifier takes over a minute to execute or crashes 【发布时间】:2014-01-09 21:25:56 【问题描述】:

在 Xcode 5.0.2 中,我创建了一个带有导航控制器的简单 iPhone 应用程序(源代码可用 at GitHub),它首先显示带有 Facebook oAuth dialog 的 Web 视图,并且 - 一旦用户通过身份验证并且她的详细信息已获取 - 尝试推送 DetailViewController:

在上面的故事板中,我调用了segue segue,并通过以下代码将其推送到ViewController.m:

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

NSLog(@"id: %@", dict[@"id"]);
NSLog(@"first_name: %@", dict[@"first_name"]);
NSLog(@"last_name: %@", dict[@"last_name"]);
NSLog(@"gender: %@", dict[@"gender"]);
NSLog(@"city: %@", dict[@"location"][@"name"]);

Facebook 用户数据被很好地提取并几乎立即打印出来:

id: 597287941
first_name: Alexander
last_name: Farber
gender: male
city: Bochum, Germany

但是在 segue 发生之前大约一分钟过去了,或者有时应用程序甚至崩溃:

2014-01-09 22:11:29.017 oauthFacebook[4012:1403] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<DetailViewController 0x8a89cd0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key id.'
*** First throw call stack:
(
    0   CoreFoundation                      0x0173b5e4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x014be8b6 objc_exception_throw + 44
    2   CoreFoundation                      0x017cb6a1 -[NSException raise] + 17
    3   Foundation                          0x0117f9ee -[NSObject(NSKeyValueCoding) setValue:forUndefinedKey:] + 282
    4   Foundation                          0x010ebcfb _NSSetUsingKeyValueSetter + 88
    5   Foundation                          0x010eb253 -[NSObject(NSKeyValueCoding) setValue:forKey:] + 267
    6   Foundation                          0x0114d70a -[NSObject(NSKeyValueCoding) setValue:forKeyPath:] + 412
    7   UIKit                               0x004cea15 -[UIRuntimeOutletConnection connect] + 106
    8   libobjc.A.dylib                     0x014d07d2 -[NSObject performSelector:] + 62
    9   CoreFoundation                      0x01736b6a -[NSArray makeObjectsPerformSelector:] + 314
    10  UIKit                               0x004cd56e -[UINib instantiateWithOwner:options:] + 1417
    11  UIKit                               0x0033f605 -[UIViewController _loadViewFromNibNamed:bundle:] + 280
    12  UIKit                               0x0033fdad -[UIViewController loadView] + 302
    13  UIKit                               0x003400ae -[UIViewController loadViewIfRequired] + 78
    14  UIKit                               0x003405b4 -[UIViewController view] + 35
    15  UIKit                               0x0035a3e2 -[UINavigationController _startCustomTransition:] + 778
    16  UIKit                               0x003670c7 -[UINavigationController _startDeferredTransitionIfNeeded:] + 688
    17  UIKit                               0x00367cb9 -[UINavigationController __viewWillLayoutSubviews] + 57
    18  UIKit                               0x004a1181 -[UILayoutContainerView layoutSubviews] + 213
    19  UIKit                               0x00297267 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 355
    20  libobjc.A.dylib                     0x014d081f -[NSObject performSelector:withObject:] + 70
    21  QuartzCore                          0x03b492ea -[CALayer layoutSublayers] + 148
    22  QuartzCore                          0x03b3d0d4 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
    23  QuartzCore                          0x03b3cf40 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 26
    24  QuartzCore                          0x03aa4ae6 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 294
    25  QuartzCore                          0x03aa5e71 _ZN2CA11Transaction6commitEv + 393
    26  QuartzCore                          0x03aa60c2 _ZN2CA11Transaction14release_threadEPv + 226
    27  libsystem_pthread.dylib             0x01e7681c _pthread_tsd_cleanup + 93
    28  libsystem_pthread.dylib             0x01e7327e _pthread_exit + 108
    29  libsystem_pthread.dylib             0x01e73dd8 pthread_workqueue_setdispatchoffset_np + 0
    30  libsystem_pthread.dylib             0x01e77cce start_wqthread + 30
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

请问这种行为的原因是什么?

更新:

我已按照 Savana 的建议清理了 IBOutlets(谢谢),但应用仍然崩溃:

2014-01-10 10:26:59.379 oauthFacebook[4342:4113] bool _WebTryThreadLock(bool), 0x8be2960: 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   0x51481ae WebThreadLock
2   0x452a57 -[UIWebDocumentView _responderForBecomeFirstResponder]
3   0x27da9e -[UIView setUserInteractionEnabled:]
4   0x934d72 -[_UIViewControllerTransitionContext _disableInteractionForViews:]
5   0x368573 -[UINavigationController pushViewController:transition:forceImmediate:]
6   0x3680b5 -[UINavigationController pushViewController:animated:]
7   0x770c65 -[UIStoryboardPushSegue perform]
8   0x76107e -[UIStoryboardSegueTemplate _perform:]
9   0x342280 -[UIViewController performSegueWithIdentifier:sender:]
10  0x29f0 __32-[ViewController fetchFacebook:]_block_invoke
11  0x11da695 __67+[NSURLConnection sendAsynchronousRequest:queue:completionHandler:]_block_invoke_2
12  0x113a945 -[NSBlockOperation main]
13  0x1193829 -[__NSOperationInternal _start:]
14  0x1110558 -[NSOperation start]
15  0x1195af4 __NSOQSchedule_f
16  0x1ae74b0 _dispatch_client_callout
17  0x1ad4018 _dispatch_async_redirect_invoke
18  0x1ae74b0 _dispatch_client_callout
19  0x1ad5eeb _dispatch_root_queue_drain
20  0x1ad6137 _dispatch_worker_thread2
21  0x1e73dab _pthread_wqthread
22  0x1e77cce start_wqthread
(lldb) 

而且当我在performSegueWithIdentifier 调用之前添加assert([NSThread isMainThread]); 时,我可以看到这不是主线程...

调用performSegueWithIdentifier是否需要在主线程上?

【问题讨论】:

问题出在您的 DetailViewController 中。这可能是一些事情......但首先我会检查 DetailViewController 中的 IBOutlet 和 IBActions,以确保您没有连接到不存在的 IBOutlet 或 IBAction。 【参考方案1】:

我已经能够使用以下代码解决我的问题(详细视图控制器在延迟后显示或应用程序甚至崩溃):

         dispatch_async(dispatch_get_main_queue(), ^(void) 
             [self performSegueWithIdentifier: @"segue" sender: self];
         );

【讨论】:

作为建议,切勿在非主要队列中进行与 UI 相关的调用。【参考方案2】:

我已经下载并详细检查了你的故事板,查看控制器从第一个文本字段到用户 ID 和 ID 的出口,但代码中没有 IBoutlet,删除故事板中的 ID IBOutlet

【讨论】:

好的,谢谢 - 但我还没有使用这些网点(只需使用NSLog 打印数据)。这可能是线程问题 - 因为我从 Facebook 回调中调用 performSegueWithIdentifier 不,一切都很好,取下插座,它就像一个魅力

以上是关于performSegueWithIdentifier 需要超过一分钟才能执行或崩溃的主要内容,如果未能解决你的问题,请参考以下文章

performSegueWithIdentifier 出现运行时错误,为啥?

iOS8 performSegueWithIdentifier 运行太慢

performSegueWithIdentifier:sender: 找不到我的转场

unwind segue / performSegueWithIdentifier: 问题

performSegueWithIdentifier 不起作用

performSegueWithIdentifier:从 didSelectRowAtIIndexPath 调用的方法缓慢显示视图