setNeedsDisplayInMapRect doesn't trigger new drawMapRect: call

后端 未结 2 455
情歌与酒
情歌与酒 2020-12-16 21:18

I\'m using a custom MKOverlay/MKOverlayView to completely cover the Google basemap with my own tiles, which are loaded asynchronously. I follow the pattern of

2条回答
  •  春和景丽
    2020-12-16 22:20

    I had an issue very similar to the one described here. In my case I couldn't reproduce the desired behaviour (described in http://developer.apple.com/library/ios/documentation/MapKit/Reference/MKOverlayView_class/Reference/Reference.html#//apple_ref/occ/instm/MKOverlayView/setNeedsDisplayInMapRect:zoomScale:) even having the most simple code possible:

    - (BOOL)canDrawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale {
        NSLog(@"This should trace forever");
    
        [self setNeedsDisplayInMapRect:mapRect zoomScale:zoomScale];
        return NO;
    }
    

    or closer to my original code:

    - (BOOL)canDrawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale {
        NSLog(@"This should trace forever");
    
        [SomeAsynchronousRequestWithCompletionHandler:^{
            [self setNeedsDisplayInMapRect:mapRect zoomScale:zoomScale];
        }];
        return NO;
    }
    

    In both cases setNeedsDisplayInMapRect:zoomScale: has never been called even once.

    The situation changed, when I began running setNeedsDisplayInMapRect:zoomScale: inside a dispatch_async dispatched to the same queue that canDrawMapRect runs on, like:

    - (BOOL)canDrawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale {
    
        dispatch_queue_t queue = dispatch_get_current_queue();
    
        NSLog(@"This should trace forever");
    
        dispatch_async(queue, ^{
            [self setNeedsDisplayInMapRect:mapRect zoomScale:zoomScale];
        });
    
        return NO;
    }
    

    or with asynchronous job included:

    - (BOOL)canDrawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale {
        NSLog(@"This should trace forever");
    
        dispatch_queue_t queue = dispatch_get_current_queue();
    
        [SomeAsynchronousRequestWithCompletionHandler:^{
            dispatch_async(queue, ^{
                [self setNeedsDisplayInMapRect:mapRect zoomScale:zoomScale];
            });
        }];
        return NO;
    }
    

    Using dispatch_async - I can see "This should trace forever" string being traced endlessly. My original problem is also disappeared completely.

    LATER UPDATE: Currently, I use dispatch_get_main_queue() to call setNeedsDisplayInMapRect:zoomScale: like

    - (BOOL)canDrawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale {
        NSLog(@"This should trace forever");
    
        [SomeAsynchronousRequestWithCompletionHandler:^{
            dispatch_async(dispatch_get_main_queue(), ^{
                [self setNeedsDisplayInMapRect:mapRect zoomScale:zoomScale];
            });
        }];
        return NO;
    }
    

提交回复
热议问题