bigfish24/ABFRealmMapView

mapview: regionDidChangeAnimated: to get current visible data (question)

Opened this issue · 0 comments

Hi, I want to get the currently visible data (annotations) on the map always when users drags/zooms the map using the delegate method:

- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
    DLog(@"regionDidChangeAnimated %lu",
         (unsigned long)[((ABFRealmMapView*)mapView).fetchResultsController.safeObjects count],
}

It works well except the results count is one step backwards. Like the count of visible annotations before I have swiped or zoomed the map. (Probably because before is this method called on delegate, it is called internally and it calls refreshMapView which is probably async - otherwise I don't know why the count should be back one step)

I noticed there is also this delegate method:

- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views
{
    DLog(@"didAddAnnotationViews %lu", (unsigned long)[((ABFRealmMapView*)mapView).fetchResultsController.safeObjects count]);
}

This seemed to return correct number of annotations. But I have noticed this is also not solution, because if there is for example on the map 10 annotations, 2 on the right side, and I drag the map to the left side (8 annotations will become hidden, 2 will remain, nothing will be added) then the method above will not be called, because 10 annotations has been already added before the drag.

So the only solution at moment I have is async delayed call like this: (which returns correct number of results)

- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

        DLog(@"regionDidChangeAnimatedAfter %lu",
             (unsigned long)[((ABFRealmMapView*)mapView).fetchResultsController.safeObjects count])
                 });
}

Or do you have any other solution how to get the list of visible items on the map after user drags/zooms the map?

Drawback of my solution is, that 1 second is quite long for update, but if I use shorter delay and there is lot of data, it can be too soon.

Thx

Otherwise your code is great and I am happy that it is so easy to withdraw even the Realm data when user drags/zooms the map. (Because the data source for my table displayed below the map also uses Realm objects, so the table can easily cooperate with the map)