【发布时间】:2017-11-28 10:31:13
【问题描述】:
我有这样的代码:
ref.child("skelbimai").observeSingleEvent(of: .value, with: {(snapshot) in
for child in (snapshot.children.allObjects as? [DataSnapshot])!{
let pinas = PinColorAnotation(color: UIColor.red)
pinas.coordinate = CLLocationCoordinate2D(latitude: child.childSnapshot(forPath: "lat").value as! Double, longitude: child.childSnapshot(forPath: "lon").value as! Double)
pinas.pinColor = Kategorija(child.childSnapshot(forPath: "cat").value as! Int).Color
pinas.title = String(Kategorija(child.childSnapshot(forPath: "cat").value as! Int).Name)
pinas.subtitle = String(child.childSnapshot(forPath: "price").value as! Double)
self.Map.addAnnotation(pinas)
}
})
问题是我无法将我的引脚保存到数组中。我有数组 Pins = PinColorAnotation,当我调用 Pins.append(pinas) 并打印 Pins.count 时,我总是得到 0。这是为什么?但是当我在循环中调用 print 时,它会显示 40。问题是我必须在从 firebase 下载后稍后对 pin 进行排序和管理。但我不能。如何解决这个问题?我需要做所有的管理逻辑吗:
ref.child("skelbimai").observeSingleEvent(of: .value, with: {(snapshot) in
for child in (snapshot.children.allObjects as? [DataSnapshot])!{
let pinas = PinColorAnotation(color: UIColor.red)
pinas.coordinate = CLLocationCoordinate2D(latitude: child.childSnapshot(forPath: "lat").value as! Double, longitude: child.childSnapshot(forPath: "lon").value as! Double)
pinas.pinColor = Kategorija(child.childSnapshot(forPath: "cat").value as! Int).Color
pinas.title = String(Kategorija(child.childSnapshot(forPath: "cat").value as! Int).Name)
pinas.subtitle = String(child.childSnapshot(forPath: "price").value as! Double)
self.Map.addAnnotation(pinas)
}
})
【问题讨论】:
-
首先,Firebase 数据是异步的,因此如果您想知道何时检索到数据,则需要一个完成处理程序。其次,你什么时候实例化 Map 数组?
标签: ios swift firebase annotations mapkit