1

First post to Stacked Overflow

I am having some difficulty with merge the MapBox database off-line content via sideloading. I have tried the examples in GitHub to no avail.

Can someone shed some light on the code snippet below I am using The file path is correct and writable The file size is 66MB so there is data in there When I call the addContents function of the MGLOfflineStorage class the pack result is zero and the content not merged.

Any ideas?

CM

import UIKit
import Mapbox

class ViewController: UIViewController, MGLMapViewDelegate {

var mapView: MGLMapView!
var progressView: UIProgressView!

override func viewDidLoad() {
    super.viewDidLoad()

    let mapView = MGLMapView(frame: view.bounds, styleURL: MGLStyle.streetsStyleURL)
    mapView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
    mapView.delegate = self
    view.addSubview(mapView)
    mapView.setCenter(CLLocationCoordinate2D(latitude: 22.27933, longitude: 114.16281),
                      zoomLevel: 13, animated: false)

    testAddFileContent()

    NotificationCenter.default.addObserver(self, selector: #selector(offlinePackProgressDidChange), name: NSNotification.Name.MGLOfflinePackProgressChanged, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(offlinePackDidReceiveError), name: NSNotification.Name.MGLOfflinePackError, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(offlinePackDidReceiveMaximumAllowedMapboxTiles), name: NSNotification.Name.MGLOfflinePackMaximumMapboxTilesReached, object: nil)

    print(MGLOfflineStorage.shared.packs?.count)

}

func testAddFileContent() {

    let documentPaths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
    let documentDir = documentPaths[0]
    let fileManager = FileManager.default

    let directoryExists: Bool = fileManager.fileExists(atPath: documentDir)
    if !directoryExists {
        try? fileManager.createDirectory(atPath: documentDir, withIntermediateDirectories: true, attributes: nil)
    }

    let bundle = Bundle.main

    // Valid database
    do {
        let resourceURL = bundle.url(forResource: "cache", withExtension: ".db")
        let filePath = bundle.path(forResource: "cache", ofType: ".db")

       // try? fileManager.moveItem(at: resourceURL! to: filePath!)
        let attributes = [FileAttributeKey.posixPermissions: NSNumber(value: 0o777)]
        try? fileManager.setAttributes(attributes, ofItemAtPath: filePath!)


        var fileSize : UInt64

        do {
            //return [FileAttributeKey : Any]
            let attr = try FileManager.default.attributesOfItem(atPath: filePath ?? "<#default value#>")
            fileSize = attr[FileAttributeKey.size] as! UInt64

            //if you convert to NSDictionary, you can get file size old way as well.
            let dict = attr as NSDictionary
            fileSize = dict.fileSize()
            print(fileSize)

        } catch {
            print("Error: \(error)")
        }

        MGLOfflineStorage.keyPathsForValuesAffectingValue(forKey: "packs")
        MGLOfflineStorage.shared.addContents(ofFile: filePath!, withCompletionHandler: nil)

        print(MGLOfflineStorage.shared.packs?.count)


     //   loadOffline()
    }
}

Merge of offline sideloaded cache.db for MapBox

  • Is your `do`/`catch` logic raising any specific errors? It could also be useful to know which version of the Maps SDK you're building with as a few have been released since you posted this. – riastrad May 29 '19 at 19:39

1 Answers1

0

I had this same issue and finally found the issue. In your main.storyboard click on the map view you are using, then click on the attribute inspector in the top right. Change the default style URL to the style URL you used when you downloaded your map region. Also change the latitude, longitude, and zoom values to match your offline region.