使用Firebase观察器和事件

时间:2017-01-18 18:44:54

标签: firebase swift3 firebase-realtime-database ios10

使用包含两个部分的应用程序 - Rider&驱动程序。当驾驶员接受请求时,会向骑车者发送一条警报,表示该请求已被接受并且驾驶员正在途中。

无法触发骑车人的警报。

RiderVC:

func driverAcceptedRequest(requestAccepted: Bool, driverName: String) {

    if !riderCancelledRequest {
        if requestAccepted {
            self.alertTheUser(title: "Ryde Accepted", message: "\(driverName) Has Accepted Your Ryde Request and will message you with details")
        } else {
            RydeHandler.Instance.cancelRyde()
            alertTheUser(title: "Ryde Cancelled", message: "\(driverName) Has Cancelled the Ryde Request")
        }
    }

    riderCancelledRequest = false
}

RydeHandler.swift:

// DRIVER ACCEPTED RYDE
    DataService.Instance.requestAcceptedRef.observe(FIRDataEventType.childAdded) { (snapshot: FIRDataSnapshot) in

        if let data = snapshot.value as? NSDictionary {
            if let name = data[Constants.NAME] as? String {
                if self.driver == "" {
                    self.driver = name
                    self.delegate?.driverAcceptedRequest(requestAccepted: true, driverName: self.driver)
                }
            }
        }
    }

Firebase数据库结构:

enter image description here

修改

tableviewcontroller中的

ViewDidLoad - 请求列表:

ref.child("drivers").child("RideRequests").observe(FIRDataEventType.value, with: { snapshot in
        self.rideRequests.removeAll()
        for item in snapshot.children{
            self.rideRequests.append(item as! FIRDataSnapshot)
        }
        self.rideRequests.reverse()
        self.tableView.reloadData()

    })

0 个答案:

没有答案