iPhone 地图视图中断
我有一个地图套件/视图,它工作正常 - 但我滚动并在 2 - 10 次移动后我的应用程序崩溃了......而这只是“中断”。
这是我的代码的一部分。我认为这是后台线程和数组释放/覆盖问题的问题。
一些背景信息:我在地图视图启动时生成一个“会话”密钥(MapKey)并在服务器端保存一个引脚。 XML 仅包含新的引脚,以实现更快的响应和更短的 XML。
// Update map when the user interacts with it
- (void)mapView:(MKMapView *)aMapView regionDidChangeAnimated:(BOOL)animated
{
MyAnnotation *annotation = [[MyAnnotation alloc] init];
MyAnnotation *ann = [[MyAnnotation alloc] init];
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
NSString *postBody = [[[NSString alloc] initWithFormat:@"single=0&lat=%f&lng=%f&sid=%@", mapView.centerCoordinate.latitude, mapView.centerCoordinate.longitude, [prefs stringForKey:@"MapKey"], [prefs stringForKey:@"MapKey"]] autorelease];
[self performSelectorInBackground:@selector(getMark:) withObject:postBody];
}
// make post and interact with verarbeiten
-(void) getMark:(NSString *)postBody
{
NSAutoreleasePool *ccpool = [[NSAutoreleasePool alloc] init];
NSString *urlStr = [[[NSString alloc] initWithFormat:@"http://URL/get.php"] autorelease];
NSMutableURLRequest *request;
NSData *postData = [postBody dataUsingEncoding:NSUTF8StringEncoding];
NSError *error;
NSURLResponse *response;
NSData *dataReply;
id stringReply;
request = [NSMutableURLRequest requestWithURL: [NSURL URLWithString:urlStr]];
[request setHTTPMethod: @"POST"];
[request setHTTPBody:postData];
[request setValue:@"text/xml" forHTTPHeaderField:@"Accept"];
dataReply = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
stringReply = (NSString *)[[[NSString alloc] initWithData:dataReply encoding:NSUTF8StringEncoding] autorelease];
[self performSelectorInBackground:@selector(verarbeiten:) withObject:stringReply];
[ccpool release];
}
//generate annotations array with annotations an set it to mapview
-(void) verarbeiten:(NSString *)stringReply
{
NSAutoreleasePool *bbpool = [[NSAutoreleasePool alloc] init];
CXMLDocument *rssParser = [[[CXMLDocument alloc] initWithXMLString:stringReply options:0 error:nil] autorelease];
NSMutableArray* annotations = [[NSMutableArray alloc] init];
NSArray *resultNodes = nil;
resultNodes = nil;
resultNodes = [rssParser nodesForXPath:@"//place" error:nil];
for (CXMLElement *resultElement in resultNodes)
{
MyAnnotation *ann = [[MyAnnotation alloc] init];
ann.title = [[resultElement childAtIndex:3] stringValue];
ann.subtitle = [[resultElement childAtIndex:5] stringValue];
ann.currentPoint = [NSNumber numberWithInt:[[[resultElement childAtIndex:1] stringValue] intValue]];
MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } };
region.center.latitude = [[[resultElement childAtIndex:9] stringValue] floatValue];
region.center.longitude = [[[resultElement childAtIndex:7] stringValue] floatValue];
ann.coordinate = region.center;
//[mapView addAnnotation:ann ];
[annotations addObject:ann];
}
[mapView addAnnotations:annotations ];
[annotations release];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
[bbpool release];
}
- (MKAnnotationView *) mapView:(MKMapView *)mV viewForAnnotation:(MyAnnotation *) annotation
{
MKPinAnnotationView *pinView = nil;
if(annotation != mapView.userLocation)
{
static NSString *defaultPinID = @"de.my.pin";
pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
if ( pinView == nil )
pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];
pinView.pinColor = MKPinAnnotationColorRed;
pinView.canShowCallout = YES;
pinView.animatesDrop = NO;
pinView.userInteractionEnabled = YES;
UIButton *btnVenue = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
btnVenue.tag = [annotation.currentPoint intValue];
[btnVenue addTarget:self action:@selector(showLinks:) forControlEvents:UIControlEventTouchUpInside];
pinView.rightCalloutAccessoryView = btnVenue;
}
else
{
[mapView.userLocation setTitle:@"You are here"];
}
return pinView;
}
#import "MyAnnotation.h"
@implementation MyAnnotation
@synthesize coordinate, title, subtitle,currentPoint;
-(void)dealloc
{
[title release];
[subtitle release];
[super dealloc];
}
@end
#import <Foundation/Foundation.h>
#import <MapKit/MKAnnotation.h>
@interface MyAnnotation : NSObject <MKAnnotation>
{
CLLocationCoordinate2D coordinate;
NSString *title;
NSString *subtitle;
NSNumber *currentPoint;
}
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;
@property(nonatomic, retain) NSNumber *currentPoint;
@end
I have a mapkit / view and it works fine - but I scroll around and after 2 - 10 moves my app crashed... and this only with a "interrupted".
Here is part of my code. I think it's a problem with the background threads and an array release / override problem.
Some background info: I generate a "session" key (MapKey) on mapview startup and save on the serverside a pin. The XML includes only new pins for a faster response and shorter XML.
// Update map when the user interacts with it
- (void)mapView:(MKMapView *)aMapView regionDidChangeAnimated:(BOOL)animated
{
MyAnnotation *annotation = [[MyAnnotation alloc] init];
MyAnnotation *ann = [[MyAnnotation alloc] init];
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
NSString *postBody = [[[NSString alloc] initWithFormat:@"single=0&lat=%f&lng=%f&sid=%@", mapView.centerCoordinate.latitude, mapView.centerCoordinate.longitude, [prefs stringForKey:@"MapKey"], [prefs stringForKey:@"MapKey"]] autorelease];
[self performSelectorInBackground:@selector(getMark:) withObject:postBody];
}
// make post and interact with verarbeiten
-(void) getMark:(NSString *)postBody
{
NSAutoreleasePool *ccpool = [[NSAutoreleasePool alloc] init];
NSString *urlStr = [[[NSString alloc] initWithFormat:@"http://URL/get.php"] autorelease];
NSMutableURLRequest *request;
NSData *postData = [postBody dataUsingEncoding:NSUTF8StringEncoding];
NSError *error;
NSURLResponse *response;
NSData *dataReply;
id stringReply;
request = [NSMutableURLRequest requestWithURL: [NSURL URLWithString:urlStr]];
[request setHTTPMethod: @"POST"];
[request setHTTPBody:postData];
[request setValue:@"text/xml" forHTTPHeaderField:@"Accept"];
dataReply = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
stringReply = (NSString *)[[[NSString alloc] initWithData:dataReply encoding:NSUTF8StringEncoding] autorelease];
[self performSelectorInBackground:@selector(verarbeiten:) withObject:stringReply];
[ccpool release];
}
//generate annotations array with annotations an set it to mapview
-(void) verarbeiten:(NSString *)stringReply
{
NSAutoreleasePool *bbpool = [[NSAutoreleasePool alloc] init];
CXMLDocument *rssParser = [[[CXMLDocument alloc] initWithXMLString:stringReply options:0 error:nil] autorelease];
NSMutableArray* annotations = [[NSMutableArray alloc] init];
NSArray *resultNodes = nil;
resultNodes = nil;
resultNodes = [rssParser nodesForXPath:@"//place" error:nil];
for (CXMLElement *resultElement in resultNodes)
{
MyAnnotation *ann = [[MyAnnotation alloc] init];
ann.title = [[resultElement childAtIndex:3] stringValue];
ann.subtitle = [[resultElement childAtIndex:5] stringValue];
ann.currentPoint = [NSNumber numberWithInt:[[[resultElement childAtIndex:1] stringValue] intValue]];
MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } };
region.center.latitude = [[[resultElement childAtIndex:9] stringValue] floatValue];
region.center.longitude = [[[resultElement childAtIndex:7] stringValue] floatValue];
ann.coordinate = region.center;
//[mapView addAnnotation:ann ];
[annotations addObject:ann];
}
[mapView addAnnotations:annotations ];
[annotations release];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
[bbpool release];
}
- (MKAnnotationView *) mapView:(MKMapView *)mV viewForAnnotation:(MyAnnotation *) annotation
{
MKPinAnnotationView *pinView = nil;
if(annotation != mapView.userLocation)
{
static NSString *defaultPinID = @"de.my.pin";
pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
if ( pinView == nil )
pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];
pinView.pinColor = MKPinAnnotationColorRed;
pinView.canShowCallout = YES;
pinView.animatesDrop = NO;
pinView.userInteractionEnabled = YES;
UIButton *btnVenue = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
btnVenue.tag = [annotation.currentPoint intValue];
[btnVenue addTarget:self action:@selector(showLinks:) forControlEvents:UIControlEventTouchUpInside];
pinView.rightCalloutAccessoryView = btnVenue;
}
else
{
[mapView.userLocation setTitle:@"You are here"];
}
return pinView;
}
#import "MyAnnotation.h"
@implementation MyAnnotation
@synthesize coordinate, title, subtitle,currentPoint;
-(void)dealloc
{
[title release];
[subtitle release];
[super dealloc];
}
@end
#import <Foundation/Foundation.h>
#import <MapKit/MKAnnotation.h>
@interface MyAnnotation : NSObject <MKAnnotation>
{
CLLocationCoordinate2D coordinate;
NSString *title;
NSString *subtitle;
NSNumber *currentPoint;
}
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;
@property(nonatomic, retain) NSNumber *currentPoint;
@end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只是一个想法:由于 -[MKMapView addAnnotations:] (可能)执行 UI 修改,您可能想在主线程中调用它:
Just a thought: since -[MKMapView addAnnotations:] (potentially) performs UI modifications, you may want to call it in the main thread: