cancelPreviousPerformRequestWithTarget 不会取消我之前使用 PerformSelector 启动的延迟线程
我已经使用 performSelector
启动了一个延迟线程,但用户仍然可以点击当前视图上的后退按钮,从而导致调用 dealloc。当发生这种情况时,我的线程似乎仍然被调用,这导致我的应用程序崩溃,因为线程尝试写入的属性已被释放。为了解决这个问题,我尝试调用 cancelPreviousPerformRequestsWithTarget
来取消之前的请求,但它似乎不起作用。下面是一些代码片段。
- (void) viewDidLoad {
[self performSelector:@selector(myStopUpdatingLocation) withObject:nil afterDelay:6];
}
- (void)viewWillDisappear:(BOOL)animated {
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(myStopUpdatingLocation) object:nil];
}
我在这里做错了什么吗?方法 myStopUpdatingLocation
是在我调用执行请求的同一类中定义的。
更多背景知识。我想要实现的功能是找到用户位置,在谷歌中搜索该位置周围的一些位置,并在地图上显示一些注释。在 viewDidLoad
上,我开始使用 CLLocationManager
更新位置。如果我在超时内没有达到所需的准确度,我会在 6 秒后建立一个超时,并且我使用 performSelector
来执行此操作。可能发生的情况是,用户单击视图中的后退按钮,即使我的所有属性已被释放,导致崩溃,该线程仍将执行。
提前致谢!
詹姆斯
I've launched a delayed thread using performSelector
but the user still has the ability to hit the back button on the current view causing dealloc to be called. When this happens my thread still seems to be called which causes my app to crash because the properties that thread is trying to write to have been released. To solve this I am trying to call cancelPreviousPerformRequestsWithTarget
to cancel the previous request but it doesn't seem to be working. Below are some code snippets.
- (void) viewDidLoad {
[self performSelector:@selector(myStopUpdatingLocation) withObject:nil afterDelay:6];
}
- (void)viewWillDisappear:(BOOL)animated {
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(myStopUpdatingLocation) object:nil];
}
Am I doing something incorrect here? The method myStopUpdatingLocation
is defined in the same class that I'm calling the perform requests.
A little more background. The function that I'm trying to implement is to find a users location, search google for some locations around that location and display several annotations on the map. On viewDidLoad
I start updating the location with CLLocationManager
. I've build in a timeout after 6 seconds if I don't get my desired accuracy within the timeout and I'm using a performSelector
to do this. What can happen is the user clicks the back button in the view and this thread will still execute even though all my properties have been released causing a crash.
Thanks in advance!
James
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我发现了我的问题,它与我对 PerformSelector 的调用没有任何关系。我发现你必须在释放 MKMapView 和 CLlocationManager 的 delgate 之前将它们设置为 nil 。否则,即使您已经释放了实例,它们仍将继续工作,并且它们有可能使您的应用程序崩溃。
感谢您的帮助诺亚!
I found my issue, it didn't have anything to do with my calls to performSelector. I found that you have to set your MKMapView and CLlocationManager's delgate to nil before releasing them. Otherwise they will continue working even though you've released the instances and they have the potential to crash your app.
Thanks for your help Noah!.
我遇到了类似的问题,我没有意识到我正在对不同的对象安排多个执行选择器调用,因此“自我”在每种情况下都是不同的。
我建议放入 NSLog(@"Self: %@",self);在每段代码之前,例如:
这将允许您比较 SELF 实例,以确保您正在同一对象上执行和释放选择器。
I ran into a similar issue where I was unaware that I was scheduling multiple performSelector calls on different objects so the "self" was different in each case.
I'd recommend throwing in a NSLog(@"Self: %@",self); before each of your bits of code such as:
That will allow you to compare the SELF instances to ensure you are performing and releasing the selectors on the same object.