简体   繁体   English

模态展示另一个控制器时,如何完全关闭视图控制器的所有后台工作

[英]How to fully close all background work of a view controller when modally presenting another one

I have 2 view controllers, in the first one I make calculations that repeat in an endless loop. 我有2个视图控制器,在第一个视图控制器中,我进行无穷循环的重复计算。

The problem is that I want to close the method and everything related to the first method when presenting the second one. 问题是在介绍第二种方法时,我想关闭该方法以及与第一种方法有关的所有内容。 Also I am conforming to MKMapViewDelegate that is triggered everytime that user location changes, where I start a background thread work. 另外,我符合每次用户位置更改时都会触发的MKMapViewDelegate ,在此我开始后台线程工作。

Now when presenting the other view controller, I want to get rid of this and break all the operations that were being executed. 现在,当介绍另一个视图控制器时,我想摆脱它并中断所有正在执行的操作。

I tried to set the delegates to nil, but when turn back to the first one the methods return and gives crash by saying 我试图将委托设置为nil,但是当回到第一个委托时,方法返回并通过说使崩溃

"Collection <__NSArrayM: 0x17f9b100> was mutated while being enumerated."

This is the function where I make calculations, the array has too many objects in it and takes about 10 sec to fully check this method. 这是我进行计算的函数,数组中包含太多对象,大约需要10秒才能完全检查此方法。

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [self makeCalculations];
    });
}

-(void)makeCalculations {
    BOOL okClicked = NO;

    for(NSDictionary *item in array) {
        NSInteger responseCode = [[item objectForKey:@"responseCode"] integerValue];

        okClicked = (responseCode > 0);

        if (okClicked) {
            dispatch_async(dispatch_get_main_queue(), ^{
                UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"title" message:@"message" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
                alert.tag =10;
                [alert show];
            });
        }
    }
}

Is there any clue and can you provide me an example or suggestion? 有什么线索,可以给我一个例子或建议吗?

Keep a counter and increment it in the loop. 保留一个计数器并在循环中递增。 Then use something like: 然后使用类似:

if(counter % 100 == 0) {
    if(self.cancelled) {
        self.cancelled = NO;
        return;
    }
}

Now, just set the cancelled BOOL when you present the new modal. 现在,当您呈现新模态时,只需设置已cancelled BOOL

You don't strictly need the counter, you could just check the flag each time... 您不必严格要求柜台,您每次都可以检查标志...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Swift:以模态方式呈现视图 controller 时更改背景颜色 - Swift: background color is changed when presenting view controller modally 模态呈现视图控制器 - Presenting a View Controller Modally 以模态呈现另一个 UIViewController 时不要关闭键盘 - Don't close keyboard when presenting another UIViewController modally 在以模态方式呈现视图控制器时,如何将视图控制器从搜索结果推入导航堆栈? - How do I push a view controller onto the nav stack from search results when presenting them modally? 呈现视图控制器以模态重置当前视图 - Presenting a View Controller Modally Resets The Current View 模态呈现控制器时,如何保持UINavigationController的UINavigationBar透明? - How to keep UINavigationController's UINavigationBar transparent when modally presenting a controller? 在导航堆栈中模态显示视图控制器 - Presenting a view controller modally within a navigation stack 通过手势识别器显示视图控制器时,“应用程序试图以模态显示主动控制器” - “'Application tried to present modally an active controller” when presenting a view controller via a Gesture Recognizer 在展示另一个控制器之前将其关闭 - dismiss view controller before presenting another one iOS 关闭模态呈现视图时黑屏 controller - iOS Black screen when close modally presented view controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM