Автономное слияние MapBox с боковой загрузкой - PullRequest
0 голосов
/ 02 апреля 2019

Первая публикация в Stacked Overflow

У меня возникли некоторые проблемы с объединением автономного содержимого базы данных MapBox через боковую загрузку.Я пробовал примеры в GitHub безрезультатно.

Может кто-нибудь пролить свет на фрагмент кода, который я использую ниже Путь к файлу правильный и записываемый Размер файла 66 МБ, поэтому там есть данные Когда явызовите функцию addContents класса MGLOfflineStorage, результат упаковки будет равен нулю, а содержимое не объединено.

Есть идеи?

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()
    }
}

Слияние автономно загруженного cache.db для MapBox

...