From 3c0e56aeafdb2f97fa2bea1cd1e6a9074983d661 Mon Sep 17 00:00:00 2001 From: Austin Payne Date: Wed, 14 Feb 2024 23:10:30 -0700 Subject: [PATCH] improvement: avoid duplicate map tile loading Previously a map tile cache miss would cause 2x loading of the tile: once from the remote tile server (which is then written to disk) and once from disk during the default MKTileOverlay.loadTile function. Instead we now directly implement loadTile so that we can avoid the duplicate loading and simply return the fetched remote tile after it is cached, which leads to a noticeable improvement in offline map performance. --- .../Helpers/Map/OfflineTileManager.swift | 30 +++++++++++-------- Meshtastic/Helpers/Map/TileOverlay.swift | 4 ++- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/Meshtastic/Helpers/Map/OfflineTileManager.swift b/Meshtastic/Helpers/Map/OfflineTileManager.swift index 709517c2..269cf9f4 100644 --- a/Meshtastic/Helpers/Map/OfflineTileManager.swift +++ b/Meshtastic/Helpers/Map/OfflineTileManager.swift @@ -103,20 +103,26 @@ class OfflineTileManager: ObservableObject { } } } - func getTileOverlay(for path: MKTileOverlayPath) -> URL { - let file = "\(UserDefaults.mapTileServer.id)-z\(path.z)x\(path.x)y\(path.y).png" - // Check is tile is already available - let tilesUrl = documentsDirectory.appendingPathComponent("tiles").appendingPathComponent(file) - if fileManager.fileExists(atPath: tilesUrl.path) { - return tilesUrl - } else { - if UserDefaults.enableOfflineMaps, UserDefaults.mapTileServer.zoomRange.contains(path.z) { // Get and persist newTile - return persistLocally(path: path) - } else { // Else display empty tile (transparent over Maps tiles) - return Bundle.main.url(forResource: "alpha", withExtension: "png")! - } + + func loadAndCacheTileOverlay(for path: MKTileOverlayPath) throws -> Data { + guard UserDefaults.enableOfflineMaps, UserDefaults.mapTileServer.zoomRange.contains(path.z) else { + return try Data(contentsOf: Bundle.main.url(forResource: "alpha", withExtension: "png")!) + } + + let tilesUrl = documentsDirectory + .appendingPathComponent("tiles") + .appendingPathComponent("\(UserDefaults.mapTileServer.id)-z\(path.z)x\(path.x)y\(path.y)") + .appendingPathExtension("png") + + do { + return try Data(contentsOf: tilesUrl) + } catch let error as NSError where error.code == NSFileReadNoSuchFileError { + let data = try Data(contentsOf: overlay.url(forTilePath: path)) + try data.write(to: tilesUrl) + return data } } + // MARK: Private methods private func computeTileOverlayPaths(boundingBox box: MKMapRect, maxZ: Int = 17) -> [MKTileOverlayPath] { var paths = [MKTileOverlayPath]() diff --git a/Meshtastic/Helpers/Map/TileOverlay.swift b/Meshtastic/Helpers/Map/TileOverlay.swift index 020e30a9..f70befb8 100644 --- a/Meshtastic/Helpers/Map/TileOverlay.swift +++ b/Meshtastic/Helpers/Map/TileOverlay.swift @@ -11,5 +11,7 @@ import MapKit typealias TileCoordinates = (x: Int, y: Int, z: Int) class TileOverlay: MKTileOverlay { - override func url(forTilePath path: MKTileOverlayPath) -> URL { OfflineTileManager.shared.getTileOverlay(for: path) } + override func loadTile(at path: MKTileOverlayPath) async throws -> Data { + return try OfflineTileManager.shared.loadAndCacheTileOverlay(for: path) + } }