I have city names and coordinates stored on my core data database and trying to get the state. In order to do this I am reverse geocoding like this
code moved below
The problem is the locationStr
object is not getting stored in the tempCities
array. I have tested inside the block and I know the locationStr
is getting created and exists.
I've been trying to figure this out for hours. Can someone clear this up for me?
Tell me if you need any other info.
EDIT:
The array is being used to fill a table view. The code is in a helper method which returns an array (the tempCities
array). I'm checking the array against nil and 0 right after the for loop.
Heres what the UISearchControllerDelegate
method looks like in the View controller
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController
{
NSString *searchText = searchController.searchBar.text;
if ([searchText length] >= 3)
{
self.resultsArray = [[[CityHelper sharedInstance]testWithSearch:searchText]mutableCopy];
[self.resultsTableView reloadData];
}
}
And in the CityHelper
class
- (NSArray *) testWithSearch: (NSString *)search
{
NSArray *cities = [self getCitiesStartingWith:search];//stores NSManagedObject subclass instances with cityName, lat, and long.
NSArray *coords = [self coordinatesForCities:cities];
NSMutableArray *tempCities = [NSMutableArray new];
for (MMCoordinate *coordinate in coords) {
CLGeocoder *geocoder = [CLGeocoder new];
CLLocation *location = [[CLLocation alloc]initWithLatitude:[coordinate.latitude floatValue]
longitude:[coordinate.longitude floatValue]];
if (![geocoder isGeocoding]) {
[geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
CLPlacemark *placemark = placemarks[0];
NSString *locationStr = [NSString stringWithFormat:@"%@, %@", placemark.locality, placemark.administrativeArea];
[tempCities addObject:locationStr];
}];
}
}
if (!tempCities) {
NSLog(@"its nil");
}
if ([tempCities count] == 0) {
NSLog(@"its 0");
}
return tempCities;
}
This always returns an empty (0 count) array