vendredi 27 février 2015

SQLite memory spikes inside Mapkit

I have a sqlite file that contains approximately 600,000 coordinates. My map takes these coordinates and annotates based on the zoom level and visible portion of the map view. When I access a column I get spikes of 50-100 megabytes. Putting this on top of the memory build up of the map causes serious memory issues on older devices (iPhone 4/4s). If I was able to manually release memory build up from the map I would not be in danger of crashing the app due to memory issues. Is there anything that I can do to avoid or reduce the memory surges?


The memory spikes seen in the image are occurring in the following lines of code:



enter code here CLLocationCoordinate2D location;
location.latitude =sqlite3_column_double(result, 0);
location.longitude =sqlite3_column_double(result, 1);

NSString *name = [[NSString alloc] initWithUTF8String:(const char *)sqlite3_column_text(result, 2)];
NSString *address = [[NSString alloc] initWithFormat:@"%s",sqlite3_column_text(result, 3) ];


Full method



NSMutableArray *nameArray = [[NSMutableArray alloc]init];
NSMutableArray *addressArray = [[NSMutableArray alloc]init];
if ([data getFlag:FLG_AnnotationON]== YES) {
[_mapView removeAnnotations:_mapView.annotations];



//To calculate the search bounds...
//First we need to calculate the corners of the map so we get the points
CGPoint nePoint = CGPointMake(self.mapView.bounds.origin.x + _mapView.bounds.size.width, _mapView.bounds.origin.y);

CGPoint swPoint = CGPointMake((self.mapView.bounds.origin.x), (_mapView.bounds.origin.y + _mapView.bounds.size.height));
CGPoint centerPoint = CGPointMake((self.mapView.bounds.origin.x + _mapView.bounds.size.width)/2, (self.mapView.bounds.origin.y + _mapView.bounds.size.height)/2);
NSLog(@"NE Point %@", NSStringFromCGPoint(nePoint));
NSLog(@"SW Point %@", NSStringFromCGPoint(swPoint));
NSLog(@"Center Point %@", NSStringFromCGPoint(centerPoint)); //Then transform those point into lat,lng values
CLLocationCoordinate2D neCoord;
neCoord = [_mapView convertPoint:nePoint toCoordinateFromView:_mapView];

CLLocationCoordinate2D swCoord;
swCoord = [_mapView convertPoint:swPoint toCoordinateFromView:_mapView];

CLLocationCoordinate2D centerCoord;
centerCoord = [_mapView convertPoint:centerPoint toCoordinateFromView:_mapView];

double zoomLevel = log2(360 * ((_mapView.frame.size.width/256) / _mapView.region.span.longitudeDelta)) + 1;
[_nameArrayForList removeAllObjects];
[_addressArrayForList removeAllObjects];

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

NSString *fileDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *dbPath = [fileDir stringByAppendingPathComponent:@"Locations.sqlite"];

NSFileManager *fm = [NSFileManager defaultManager];
if( [fm fileExistsAtPath:dbPath] )
{
if ([[NSThread currentThread] isCancelled])
return;

sqlite3 *db;

if (sqlite3_open([dbPath UTF8String], &db) == SQLITE_OK)
{
float Lat1,Lat2, Lon1, Lon2;

if (neCoord.latitude > swCoord.latitude)
{
Lat1 = swCoord.latitude;
Lat2 = neCoord.latitude;
}
else
{
Lat2 = swCoord.latitude;
Lat1 = neCoord.latitude;
}

if (neCoord.longitude > swCoord.longitude)
{
Lon1 = swCoord.longitude;
Lon2 = neCoord.longitude;
}
else
{
Lon2 = swCoord.longitude;
Lon1 = neCoord.longitude;
}

// select top record for specified SSID
NSString *query = [NSString stringWithFormat:@"SELECT latitude, longitude, VenueName, VenueAddress FROM Hotspots WHERE latitude > %f and latitude < %f and longitude >%f and longitude < %f and GeocodeLocationType < 3", Lat1, Lat2, Lon1, Lon2];


const char *st = [query UTF8String];

sqlite3_stmt* result;

int retcode = sqlite3_prepare_v2(db, st, -1, &result, NULL);
NSMutableArray *annotations = [[NSMutableArray alloc]init];

if(retcode ==SQLITE_OK)
{

NSLog(@"Before int = 0");
__block int i =0;
@autoreleasepool {
while(sqlite3_step(result)==SQLITE_ROW)
{


//34.057951,-118.445825
CLLocationCoordinate2D location;
location.latitude =sqlite3_column_double(result, 0);
location.longitude =sqlite3_column_double(result, 1);

NSString *name = [[NSString alloc] initWithUTF8String:(const char *)sqlite3_column_text(result, 2)];
NSString *address = [[NSString alloc] initWithFormat:@"%s",sqlite3_column_text(result, 3) ];

if (zoomLevel <12.0) {

dispatch_async(dispatch_get_main_queue(), ^{

});

}


if (zoomLevel >=16)
{

dispatch_async(dispatch_get_main_queue(), ^{




i++;
NSLog(@"%i", i);





OffCustomAnnotation *customAnnotation = [[OffCustomAnnotation alloc ]initWithName:name address:address coordinate:location];
[self.mapView addAnnotation:customAnnotation];

[self milesFromCenter:centerCoord andToHotspot:hotspot];
[nameArray addObject:customAnnotation.title];
[addressArray addObject:customAnnotation.subtitle];

});
}
if (zoomLevel >=12 && zoomLevel < 16)
{

dispatch_async(dispatch_get_main_queue(), ^{

//[_mapView removeOverlays:_mapView.overlays];

i++;
if (i<50) {
SmCustomAnnotation *smallAnnotation = [[SmCustomAnnotation alloc]initWithName:name address:address coordinate:hotspot];
//[annotations addObject:smallAnnotation];
//
[self.mapView addAnnotation:smallAnnotation];
NSLog(@"Annotation: %@", smallAnnotation.title);
[nameArray addObject:smallAnnotation.title];
[addressArray addObject:smallAnnotation.subtitle];

allowTbVCellSelection = NO;

NSLog(@"%i", i);

// MKCircle *circle = [MKCircle circleWithCenterCoordinate:hotspot radius:20];
// [self.mapView addOverlay:circle];
// SmCustomAnnotation *locationOfhotspot = [[SmCustomAnnotation alloc ]initWithName:name address:address coordinate:hotspot];
// [annotations addObject:locationOfhotspot];
}


});
}

}

}
sqlite3_finalize(result);


// [_spinner stopAnimating];

}
else
{
int ext_error = sqlite3_extended_errcode(db);
}
}


sqlite3_close(db);
dispatch_async(dispatch_get_main_queue(), ^{

[_nameArrayForList addObjectsFromArray:nameArray];
[_addressArrayForList addObjectsFromArray:addressArray];
[self.tableview reloadData];

//[self.mapView addAnnotations: annotations];
[_redoMapButton setHidden:YES];
[_spinner stopAnimating];
// [_spinner stopAnimating];
});

}
else
{
//[logfile writeTag:@"Data" andType:LOG_Error andMessage:@"Unable to open database."];

}




});

}


Memory Spikes


Aucun commentaire:

Enregistrer un commentaire