Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/107.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 目标C-Iphone应用程序开发。如果主线程挂起,我们如何终止应用程序?_Ios_Objective C_Iphone_Xcode - Fatal编程技术网

Ios 目标C-Iphone应用程序开发。如果主线程挂起,我们如何终止应用程序?

Ios 目标C-Iphone应用程序开发。如果主线程挂起,我们如何终止应用程序?,ios,objective-c,iphone,xcode,Ios,Objective C,Iphone,Xcode,我的主要目标是,如果dispatch\u get\u Main\u queue()-hung¬响应,则终止应用程序 我有这段代码 dispatchSource = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_

我的主要目标是,如果dispatch\u get\u Main\u queue()-hung¬响应,则终止应用程序

我有这段代码

dispatchSource = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,
                                                          dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0));

double interval = 10.0;
dispatch_time_t startTime = dispatch_time(DISPATCH_TIME_NOW, 0);
uint64_t intervalTime = (int64_t)(interval * NSEC_PER_SEC);
dispatch_source_set_timer(dispatchSource, startTime + intervalTime, intervalTime, 0);

dispatch_source_set_event_handler(dispatchSource, ^{
    NSLog(@"Main Thread not reponding for 10 secs. Capture App terminated");
    exit(0);
});
NSLog(@"LOW");
dispatch_resume(dispatchSource);

dispatch_async(dispatch_get_main_queue(), ^{
    dispatch_source_cancel(dispatchSource);
这是一个很好的XCode模拟器,但在实际设备中并没有达到预期效果

为什么这段代码在实际设备中不起作用

我们使用UIWebView加载页面。页面上有一些flash视频。尝试加载时,应用程序会挂起。那个网页不属于我们。是的,我同意,这是避免它卡住的理想方法。但我们希望能从这个问题中找到一个解决方案或解决方法


如果有任何其他方法来识别dispatch\u get\u main\u queue()需要时间-并关闭它,这将更有帮助。

这不是正确的方法。但如果你知道自己做错了,但仍然想做错事,请使用下面的方法

  • 有这样的功能,

    - (void)terminateApp:(NSTimer*)timer
    {
       if(mainQueueDispatchSuccessful)
       {
           [timer invalidate];
           timer = nil;
       }
       else
       {
           NSString *reasonString = @"Main thread not responding at SBLAppDelegate for 10 secs. Force Closing app.";
           NSLog(@"%@",reasonString);
           exit(0);
       }
    }
    
  • 像这样在主队列调度之前和之后使用它

        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            t = [NSTimer scheduledTimerWithTimeInterval:10 target:self selector:@selector(terminateApp:) userInfo:nil repeats:NO];
            [[NSRunLoop currentRunLoop] addTimer:t forMode:NSDefaultRunLoopMode];
            [[NSRunLoop currentRunLoop] run];
        });
    
        mainQueueDispatchSuccessful = false;
    
        dispatch_async(dispatch_get_main_queue(), ^{
    
            mainQueueDispatchSuccessful = true;
            .......
            .......
            }
    

  • 谢谢你的帮助。

    你为什么这么做?这不是苹果希望你做事情的方式。他们希望你不要管它,尽一切努力确保主线程不会卡住。还有为什么它会卡住?您可以调用
    exit(0)
    ,但显然不建议这样做。它是用户崩溃的同义词。@特洛伊木马我们使用webview加载页面。页面上有一些flash视频。尝试加载时,应用程序会挂起。那个网页不属于我们。是的,我同意,这是避免它卡住的理想方法。但我们想从中找到一个解决方案或破解方法problem@Larme我们正在使用出口(0)。但我们只需要在dispatch_get_main_queue()未响应时执行。如果主线程挂起,iOS将为您终止应用程序。。。因为它已经崩溃了。不要这样做。不要依赖这个。不要挂主线。曾经