在 MKMapview RegionDidChangeAnimated 方法中使用 NSAutoreleasepool 时应用程序崩溃
我正在开发一个地图应用程序,因为我喜欢在用户更改地图视图时放下图钉(就像在 Zillow 应用程序中一样)。我正在使用以下代码。我尝试使用 NSAutoreleasepool 从服务器加载 xml 数据,以在后台线程中进行 xml 解析。
(void)mapView:(MKMapView *)mapView区域DidChangeAnimated:(BOOL)动画{
NSLog(@"区域内部确实发生了变化");
urlString =[NSString stringWithFormat: @"http://asdfasdasdf.com/asdfasdf/mapxml.php]; [故事1发布]; [mapview removeAnnotations:eventPoints1]; eventPoints1 = [[NSMutableArray 数组] 保留]; [self PerformSelectorInBackground:@selector(callParsing) withObject:nil];
}
-(void)callParsing{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[self parseXMLFileAtURL:urlString];
[self performSelectorOnMainThread:@selector(droppingPin) withObject:nil waitUntilDone:YES];
[pool drain];
}
上面的代码工作正常,但是一旦我更改了地图视图,应用程序就会崩溃。任何人都可以帮我解决这个问题吗?
提前致谢。
i am working on a map application, in that i like to drop the pins (as in Zillow apps) when ever user change the map view. I am using following code code. i am try to load the xml data from server using NSAutoreleasepool to do the xml parsing in the background thread.
(void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated{
NSLog(@"inside region did changed ");
urlString =[NSString stringWithFormat: @"http://asdfasdasdf.com/asdfasdf/mapxml.php]; [stories1 release]; [mapview removeAnnotations:eventPoints1]; eventPoints1 = [[NSMutableArray array] retain]; [self performSelectorInBackground:@selector(callParsing) withObject:nil];
}
-(void)callParsing{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[self parseXMLFileAtURL:urlString];
[self performSelectorOnMainThread:@selector(droppingPin) withObject:nil waitUntilDone:YES];
[pool drain];
}
The above code is working fine, but once i changed the mapview, the appllication get crashed. Anyone can help me to fix the issue?
thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
urlString 从 stringWithFormat 返回时已经自动释放。
由于您在 callParsing 中使用 urlString (它在不同的线程上执行),因此您应该将其作为对象传递给该方法。否则,您将面临在执行 callParsing 方法之前释放它的风险,从而导致崩溃:
urlString is already autoreleased when it is returned from stringWithFormat.
Since you are using urlString in callParsing which is executed on a different thread, you should pass it as an object to that method. Otherwise you risk it getting released before the callParsing method is executed and thus causing the crash: