我有一些对Firebase数据库的请求,这些请求包含在signalProducer中,如下所示:

static func parseOne(snap: FIRDataSnapshot) -> SignalProducer<FUser, NSError> {
    return SignalProducer { subscriber, disposable in
        let ref = FIRDatabase.database().reference()
        let objRef = ref.child(FUser.URL + "/" + snap.key)
        objRef.observeSingleEventOfType(.Value, withBlock: { (snap) in
            let user = FUser(snap: snap)
            subscriber.sendNext(user)
            subscriber.sendCompleted()
        })
    }
}


我希望能够同时调用其中的几个,然后等待所有操作完成后再执行操作。

Reactivecocoa有办法解决这个问题吗?还是我与signalProducer方向错误?

最佳答案

这是Reactcocoa擅长的-并且有一个内置的运算符combineLatest可以完全满足您的期望。例如,parseMany函数将如下所示:

func parseMany(snaps: [FIRDataSnapShot]) -> SignalProducer<[FUser], NSError> {
    let parseOneSignals = snaps.map(parseOne) //array of FUser signal producers
    return combineLatest(parseOneSignals) //signal producer that sends .Next(arrayOfAllFUsers) when all the parseOneSignals have sent their .Next(singleFUser)
}

关于swift - Reactivecocoa:WAITING多个Firebase请求完成(快速),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37720957/

10-09 06:58