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

350 views Asked by At

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 ?

1

There are 1 answers

0
Evan Drewry On

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