[英]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.