setNeedsDisplayInMapRect doesn't trigger new drawMapRect: call

后端 未结 2 453
情歌与酒
情歌与酒 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 21:56

    The answer above did not work for me. From the NSLog printout I used I could see that a different thread was being used for despite grabbing the dispatch_get_current_queue() in canDrawMapRect and storing it for later use. This was the case in the iPad 4.3 Simulator at least, I did not attempt on the device.

    My solution was less satisfactory and more error prone solution of wait x time before calling.

    -(void)setNeedsDisplay:(WebRequest*)webRequest
    {  
       [webRequest retain];
       dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.25 * NSEC_PER_SEC), dispatch_get_main_queue(), 
       ^{
          [webRequest autorelease];
          [delegate setNeedsDisplayInMapRect:webRequest.request.requestedMapRect 
                                  zoomScale:webRequest.request.requestedScale];
       });     
    }
    
    0 讨论(0)
  • 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;
    }
    
    0 讨论(0)
提交回复
热议问题