1

In my app I fetch a coredatabase and put results into an array called self.stores. I convert those store locations to MyLocation objects which have a distance property. I plot the MyLocation objects like so:

- (void)plotStorePositions:(NSString *)responseString {

    for (id<MKAnnotation> annotation in _mapView.annotations) {
        [_mapView removeAnnotation:annotation];
    }
    //CYCLE THRU STORE OBJECTS
    for (Store * storeObject in self.stores) {

        NSString * latitude = storeObject.latitude;
        NSString * longitude = storeObject.longitude;
        NSString * storeDescription = storeObject.name;
        NSString * address = storeObject.address;

        //CREATE MYLOCATION OBJECTS
        CLLocationCoordinate2D coordinate;
        coordinate.latitude = latitude.doubleValue;
        coordinate.longitude = longitude.doubleValue;
        MyLocation *annotation = [[MyLocation alloc] initWithName:storeDescription address:address coordinate:coordinate distance:0];

        //CREATE A PIN FOR EACH MYLOCATION ANNOTATION
        CLLocation *pinLocation = [[CLLocation alloc] initWithLatitude:annotation.coordinate.latitude longitude:annotation.coordinate.longitude];

        //SET USERLOCATION (Must move this code out)
        self.userLocation = [[CLLocation alloc] initWithLatitude:self._mapView.userLocation.coordinate.latitude longitude:self._mapView.userLocation.coordinate.longitude];

        //SET USERLOCATION TO APPDELEGATE (Must move this code out)
        AppDelegate *myDelegate = [[UIApplication sharedApplication] delegate];
        myDelegate.userLocation = self.userLocation;

        //CALCULATE DISTANCE AND SET ITS THAT MYLOCATION's DISTANCE PROPERTY
        CLLocationDistance calculatedDistance = [pinLocation distanceFromLocation:userLocation];
        annotation.distance = calculatedDistance/1000;        

        [_mapView addAnnotation:annotation];

    }

}

This is in a mapview. I then have a tableview where I fetch the same coredatabase and get self.stores again. Then I cycle through that array of self.stores to create STORE storeObjects to put into my tableview cells. But I want to sort those cells. How do I get MyLocation objects which should be in memory? Do I need to pass them to the appDelegate or to the tableview controller?

marciokoko
  • 4,988
  • 8
  • 51
  • 91
  • use the right tags. do you think object tag is a proper tag to get answers about IOS development. – mpm Feb 14 '13 at 16:32
  • I'm having a hard time understanding your question. Are you asking how to sort your `self.stores` object according to the distance? – Georg Schölly Feb 14 '13 at 20:31
  • I have a mapview controller and a tableview controller. the mapviewcontroller is fine. I want the tableview cells to be ordered by distance. At present, the cells are populated from Store objects in the self.stores array. Store objects obviously don't have a distance (to userLocation) property. So in the mapviewcontroller i use that plotStores method where I put the db fetch in a self.stores array and loop thru it to create MKannotations by using a MyLocation NSObject which does have a distance property. Is there a way to just access those MyLocation objects to sort the cells? – marciokoko Feb 14 '13 at 20:43
  • What class has `self.stores`? – Georg Schölly Feb 14 '13 at 21:56
  • Both the mapviewcontroller, which is where the plot method above is from. And I do so again in the tableviewcontroller. Im actually repeating a db fetch in both viewcontrollers' viewDidLoad methods and putting its results into NSArray self.stores. – marciokoko Feb 14 '13 at 22:09
  • Do I simply set a MyLocation property on the table view controller from within the map view controller? – marciokoko Feb 15 '13 at 16:39

1 Answers1

0

As you are using Core Data, your best option is to use a NSFetchedResultsController to populate your table view.

All you need then is the managed object context, either from another controller or the app delegate.

Mundi
  • 79,884
  • 17
  • 117
  • 140