0

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 ?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Leguman
  • 2,104
  • 1
  • 18
  • 22

1 Answers1

0

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)
}
Evan Drewry
  • 794
  • 6
  • 17