From 9449b6533876c9b9ed2aa617ca679e0c3c3bad8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Minh=20Nguye=CC=82=CC=83n?= Date: Mon, 6 Apr 2020 09:40:10 -0700 Subject: [PATCH] [ios, macos] Initialize offline storage on main thread before snapshotting --- platform/darwin/src/MGLMapSnapshotter.mm | 9 +++++++-- platform/darwin/test/MGLMapSnapshotterTests.m | 4 ---- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/platform/darwin/src/MGLMapSnapshotter.mm b/platform/darwin/src/MGLMapSnapshotter.mm index 1e7547c0b1..ebab81261b 100644 --- a/platform/darwin/src/MGLMapSnapshotter.mm +++ b/platform/darwin/src/MGLMapSnapshotter.mm @@ -301,9 +301,14 @@ - (void)startWithQueue:(dispatch_queue_t)queue overlayHandler:(MGLMapSnapshotOve if (!completion) { return; } - if (!mbgl::Scheduler::GetCurrent()) { + + // Ensure that offline storage has been initialized on the main thread, as MGLMapView and MGLOfflineStorage do when used directly. + // https://github.com/mapbox/mapbox-gl-native-ios/issues/227 + if ([NSThread.currentThread isMainThread]) { + (void)[MGLOfflineStorage sharedOfflineStorage]; + } else { [NSException raise:NSInvalidArgumentException - format:@"startWithQueue:completionHandler: must be called from a thread with an active run loop."]; + format:@"-[MGLMapSnapshotter startWithQueue:completionHandler:] must be called from the main thread, not %@.", NSThread.currentThread]; } if (self.loading) { diff --git a/platform/darwin/test/MGLMapSnapshotterTests.m b/platform/darwin/test/MGLMapSnapshotterTests.m index 0d94b668cd..ef5f35ea0f 100644 --- a/platform/darwin/test/MGLMapSnapshotterTests.m +++ b/platform/darwin/test/MGLMapSnapshotterTests.m @@ -62,10 +62,6 @@ - (void)setUp { [super setUp]; [MGLAccountManager setAccessToken:@"pk.feedcafedeadbeefbadebede"]; - - // Initialize file system and run loop before snapshotting. - // https://github.com/mapbox/mapbox-gl-native-ios/issues/227 - (void)[MGLOfflineStorage sharedOfflineStorage]; } - (void)tearDown {