【发布时间】:2019-04-02 08:24:11
【问题描述】:
第一次发布到 Stacked Overflow
我在通过侧载合并 MapBox 数据库离线内容时遇到了一些困难。我已经尝试了 GitHub 中的示例,但无济于事。
有人可以解释一下我正在使用的下面的代码 sn-p 文件路径正确可写 文件大小为 66MB,所以里面有数据 当我调用 MGLOfflineStorage 类的 addContents 函数时,打包结果为零并且内容未合并。
有什么想法吗?
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()
}
}
为 MapBox 合并离线侧载 cache.db
【问题讨论】:
-
您的
do/catch逻辑是否引发任何特定错误?了解您正在构建的 Maps SDK 的哪个版本也很有用,因为自您发布此消息以来已经发布了一些版本。
标签: swift mapbox sideloading