在 MKMapview RegionDidChangeAnimated 方法中使用 NSAutoreleasepool 时应用程序崩溃

发布于 2024-09-05 13:49:30 字数 885 浏览 7 评论 0原文

我正在开发一个地图应用程序,因为我喜欢在用户更改地图视图时放下图钉(就像在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寻找我们的幸福 2024-09-12 13:49:30

urlString 从 stringWithFormat 返回时已经自动释放。
由于您在 callParsing 中使用 urlString (它在不同的线程上执行),因此您应该将其作为对象传递给该方法。否则,您将面临在执行 callParsing 方法之前释放它的风险,从而导致崩溃:

...
[self performSelectorInBackground:@selector(callParsing:) withObject:urlString];
...

-(void)callParsing:(NSString*)urlString {
...

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:

...
[self performSelectorInBackground:@selector(callParsing:) withObject:urlString];
...

-(void)callParsing:(NSString*)urlString {
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文