【问题标题】:How to handle data got from Firebase如何处理从 Firebase 获取的数据
【发布时间】: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


【解决方案1】:

这样的事情应该可以工作:

func getPins(completion: @escaping ([PinColorAnnotation]) -> Void){
  var data: [PinColorAnnotation] = []
  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.data.append(pinas)
    }
    completion(data)
})
}

然后,您可以在任何需要的地方调用该函数,例如viewDidLoad。然后可以将返回的数据设置为self.map 数组。

如果有任何错误,我深表歉意,这是在平板电脑上完成的,因为我不在电脑附近。

【讨论】:

  • 非常感谢我之前不知道完成处理程序:D
  • 不客气!如果有效,请将答案标记为正确。
猜你喜欢
  • 2020-02-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多