簡體   English   中英

模態展示另一個控制器時,如何完全關閉視圖控制器的所有后台工作

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

我有2個視圖控制器,在第一個視圖控制器中,我進行無窮循環的重復計算。

問題是在介紹第二種方法時,我想關閉該方法以及與第一種方法有關的所有內容。 另外,我符合每次用戶位置更改時都會觸發的MKMapViewDelegate ,在此我開始后台線程工作。

現在,當介紹另一個視圖控制器時,我想擺脫它並中斷所有正在執行的操作。

我試圖將委托設置為nil,但是當回到第一個委托時,方法返回並通過說使崩潰

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

這是我進行計算的函數,數組中包含太多對象,大約需要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];
            });
        }
    }
}

有什么線索,可以給我一個例子或建議嗎?

保留一個計數器並在循環中遞增。 然后使用類似:

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

現在,當您呈現新模態時,只需設置已cancelled BOOL

您不必嚴格要求櫃台,您每次都可以檢查標志...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM