简体   繁体   中英

Reactivecocoa: wait for several firebase requests to complete (swift)

I have several requests to my Firebase database that are contains in a signalProducer like this one:

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()
        })
    }
}

I would like to be able to call several of them concurrently then waiting for all to complete before doing something.

Is there way to this with Reactivecocoa ? Or am I in the wrong direction going with signalProducer ?

this is something reactivecocoa excels at- and there is a built-in operator combineLatest that does exactly what you are looking to do. for example a parseMany function would look something like this:

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)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM