I am fetching GPS information of all my images and they are stored in a Dictionary. I would pass on the lat & long values from this dictionary to the reverseGeocodeLocation
function call & store the results in my database.
The problem here is that this function is an asynchronous call & I need to synchronize the whole process for my record to get inserted into the table.
Eg: My array read following coordinates: 32.77003,96.87532. It now calls the reverseGeocodeLocation
function, passing on these coordinates as CLLocation
object. Now before this async function returns me back the geo-coded location name, next request with new set of coordinates is sent to reverseGeocodeLocation
function. This causes inconsistency to insert the record into database.
Is there any way to have this whole task turn synchronous?
i.e Make my for-loop wait until reverseGeocodeLocation
returns a value and then move on to next record to be geo-coded?
A bit of my code is here:
for (int imgIdx=0; imgIdx<[imageMetaMutArray count]; imgIdx++)
{
NSDictionary *localGpsDict = [[NSDictionary alloc]initWithDictionary: [imageMetaMutArray objectAtIndex:imgIdx]];
imgLatLoc=[localGpsDict valueForKey:@"Latitude"];
imgLongLoc=[localGpsDict valueForKey:@"Longitude"];
dateStamp=[localGpsDict valueForKey:@"DateStamp"];
timeStamp=[localGpsDict valueForKey:@"TimeStamp"];
if(imgLatLoc && imgLongLoc && dateStamp && timeStamp)
{
CLGeocoder *geoCoder=[[CLGeocoder alloc]init];
CLLocation *currentLocation=[[CLLocation alloc]initWithLatitude:[imgLatLoc doubleValue] longitude:[imgLongLoc doubleValue]];
[geoCoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placeMarks, NSError *err){
if(err)
{
NSLog(@"Reverse geo-coding failed because: %@",err);
return;
}
if(placeMarks && placeMarks.count>0)
{
CLPlacemark *placeMarkers=placeMarks[0];
NSDictionary *locationDictionary=placeMarkers.addressDictionary;
NSString *country=[locationDictionary objectForKey:(NSString *)kABPersonAddressCountryKey];
NSString *city=[locationDictionary objectForKey:(NSString *)kABPersonAddressCityKey];
NSString *state=[locationDictionary objectForKey:(NSString *)kABPersonAddressStateKey];
NSLog(@"logged in from:");
if(city)
{
NSLog(@"city: %@",city);
locName = [[NSString alloc]initWithString:city];
if(state)
{
NSLog(@"state: %@",state);
locName=[locName stringByAppendingString:@","];
locName=[locName stringByAppendingString:state];
}
if(country)
{
NSLog(@"country: %@",country);
locName=[locName stringByAppendingString:@","];
locName=[locName stringByAppendingString:country];
}
}
else
{
if(state)
{
NSLog(@"state: %@",state);
locName = [[NSString alloc]initWithString:state];
if(country)
{
NSLog(@"country: %@",country);
locName=[locName stringByAppendingString:@","];
locName=[locName stringByAppendingString:country];
}
}
else
{
NSLog(@"country: %@",country);
locName = [[NSString alloc]initWithString:country];
}
}
}
else
{
NSLog(@"Placemark Error code:: %lu\n%@",(unsigned long)placeMarks.count,placeMarks);
}
[locName retain];
NSLog(@"location decoded is: %@",locName);
/*Call for Insert into Images table*/
[self insertDataImgTbl:locName];
});
}
}
}