I'm using Mapbox-iOS-SDK 3.3.7 with Swift 3 to develop a feature that would let users download maps for offline use. The initial view will have a UITableView that will provide a list of all previously downloaded MGLOfflinePacks. I did my best to port to Swift 3 the Objective-C example that Mapbox provides for managing offline packs.
Unfortunately, I'm running into a problem where the offline packs do not appear when the table is initially displayed. However, if I push another controller onto the NavigationController or otherwise leave and return to the UITableView, the list of offline packs appears as expected.
I believe that my Notification observers and KVO are properly set up according to the documentation but no matter what I try, tableView(_ tableView: UITableView, numberOfRowsInSection section: Int)
always returns 0 until I leave the controller and return, presumably because MGLOfflineStorage.shared().packs returns nil when the code gets triggered to check it.
Here are the relevant parts of the code I'm using that I ported from the aforementioned Obj-C example:
import Mapbox
class OfflineMapTableViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
// observers for offline pack loading
MGLOfflineStorage.shared().addObserver(self, forKeyPath: "paths", options: .initial, context: nil)
NotificationCenter.default.addObserver(self, selector: #selector(offlinePackProgressDidChange), name: NSNotification.Name.MGLOfflinePackProgressChanged, object: nil)
}
deinit {
MGLOfflineStorage.shared().removeObserver(self, forKeyPath: "paths")
NotificationCenter.default.removeObserver(self)
}
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
guard let change = change else { return }
if keyPath == "paths" {
if let changeKind = change[NSKeyValueChangeKey.kindKey] as? UInt,
let kind = NSKeyValueChange(rawValue: changeKind) {
var indexPaths: [IndexPath] = []
if let indexes = change[NSKeyValueChangeKey.indexesKey] as? NSIndexSet {
if indexes.count > 0 {
indexes.enumerate({ (index, stop) in
indexPaths.append(IndexPath(row: index, section: 0))
})
}
}
switch kind {
case NSKeyValueChange.insertion:
tableView.insertRows(at: indexPaths, with: .automatic)
break
case NSKeyValueChange.removal:
tableView.deleteRows(at: indexPaths, with: .automatic)
break
case NSKeyValueChange.replacement:
tableView.reloadRows(at: indexPaths, with: .automatic)
break
default:
tableView.reloadData()
if let packs = MGLOfflineStorage.shared().packs {
for pack in packs {
if pack.state == .unknown {
pack.requestProgress()
}
}
}
break
}
}
} else {
super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
}
}
func offlinePackProgressDidChange(notification: NSNotification) {
guard let pack = notification.object as? MGLOfflinePack else { return }
guard let index = MGLOfflineStorage.shared().packs?.index(of: pack) else { return }
let indexPath: IndexPath = IndexPath(row: index, section: 0)
if let cell = tableView.cellForRow(at: indexPath) {
updateTableViewCell(cell, atIndexPath: indexPath, forPack: pack)
}
}
// MARK: - Table view data source
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if let packs = MGLOfflineStorage.shared().packs {
return packs.count
} else {
return 0
}
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "idCell", for: indexPath)
// Configure the cell...
if let pack = MGLOfflineStorage.shared().packs?[indexPath.row] {
updateTableViewCell(cell, atIndexPath: indexPath, forPack: pack)
}
return cell
}
func updateTableViewCell(_ cell: UITableViewCell, atIndexPath indexPath: IndexPath, forPack pack: MGLOfflinePack) {
if let userInfo = NSKeyedUnarchiver.unarchiveObject(with: pack.context) as? [String: String] {
if let name = userInfo["name"] {
cell.textLabel?.text = name
}
}
}
}
Anyone know why the list of offline packs only appears when I leave then return to the view controller?
Try changing
forKeyPath: "paths"
toforKeyPath: "packs"
.The name of the
MGLOfflineStorage
property ispacks
.