iOS MKMapShapshotter completion block is not always being called
Asked Answered
D

4

11

I am trying to use the new iOS7 MKMapSnapshotter to generate a static map image. Whenever my app needs a map, I call the following:

MKMapSnapshotter *snapshotter = [[[MKMapSnapshotter alloc] initWithOptions:theOptions] autorelease];
dispatch_queue_t aQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0);
DebugLog(@"Snapshotter allocated %@ and run on queue %@", snapshotter, aQueue);

[snapshotter startWithQueue:aQueue completionHandler:^(MKMapSnapshot *snapshot, NSError *error) {
    DebugLog(@"Snapshotter completion block %@", snapshotter);
    // perform selector on main thread to set self.imageView.image = shanpshot.image;
}

In most cases this is working great. However sometimes, it seems like the device gets overloaded with requests for maps and then it stops rendering. In my log file I will see the first log statement about the "Snapshotter allocated" but never see the "Snapshotter completion block" message.

Is it possible that my requests are never executed off of the dispatch queue? Has anyone ever had this problem?

Dunaville answered 31/3, 2014 at 23:25 Comment(3)
Did you find a solution? I'm facing the same problem :/Bowfin
have you tried using - (void)startWithCompletionHandler:(MKMapSnapshotCompletionHandler)completionHandler ?Madlynmadman
I would begin by creating a dedicated queue for your snapshotter instance. If that resolves reliably, you may have blocked the global background queue somewhere else in your app.Amidst
P
2

This is (or appears to be) a bug in MKMapSnapshotter.

If network data and WiFi are turned off, the completion handler will not be called (unless there are cached data in the OS - see https://mcmap.net/q/1159057/-how-can-i-clear-mkmapview-39-s-cache-of-map-tiles for clearing the cache).

In fact, the snapshotter appears to block waiting on data. It does not time out or detect that there is no data. After many minutes, e.g. 15 minutes, snapshotter.isLoading = YES. Calling cancel does not cause the completion handler to be called.

If WiFi or network data are turned back on, subsequent calls to start a (new) snapshotter will call the completion handler.

This s*cks badly if a variable is set when the snapshotter is started and cleared in the handler, because the variable is never cleared.

if (!isRendering) {
    isRendering = YES;

    [snapshotter startWithCompletionHandler:
     ^(MKMapSnapshot* snapshot, NSError* error) {
         // This may not be called so this code will
         // never run again.
         isRendering = NO;
     }];
}
Peti answered 2/9, 2015 at 5:50 Comment(0)
A
0

This issue is probably arising since snapshotter is not a property, therefore it is getting autoreleased soon after leaving this scope. Retaining snapshotter as a property should fix it.

Amati answered 26/7, 2015 at 0:52 Comment(0)
R
0

Had the same issue in different iOS versions. The point was that the AppleMaps app did not load the maps properly also. Restarting the device hard got it to work. So I suppose it is an Apple bug.

Rusty answered 2/6, 2016 at 14:55 Comment(0)
D
0

I have the same issue. The reason was in MKMapSnapshotOptions where i set very small region. I set default values for delta longitude and delta latitude as 0.05. Now it works.

Drinking answered 9/8, 2016 at 8:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.