RxSwiftCommunity / FirebaseRxSwiftExtensions

Extension Methods for Firebase and RxSwift
MIT License
77 stars 10 forks source link

Help: Combining observables into array #7

Closed danieljvdm closed 8 years ago

danieljvdm commented 8 years ago

It seem like Firebase returns a block for each child at a ref. I have an app with a table view and I want to pull it's data from a Observable<[User]>, but I get back a bunch of Observable<User>s from Firebase. How would you recommend going about this?

danieljvdm commented 8 years ago

I figured out I can use a variable and the value member!

danieljvdm commented 8 years ago

After more thought, I feel like there has to be a better way to merge the signals into one array. Do you know of any? The way I'm currently using these extensions seems like it's not really doing more than the vanilla Firebase methods are, as I have to wrap most of the results in a Variable to get a live array.

mbalex99 commented 8 years ago

Hi @danieljvdm. I'm sorry I don't seem to understand what you're doing.

Are you trying to go from let usernames = ["max", "dan", "john"] then map those to Observable<[UserModel]>?

You can use combineLatest on an [Observable] and get the changes if any of them are mapped.

danieljvdm commented 8 years ago

So when dealing with the Event Types of .ChildAdded (or .ChildAnything), the callback is fired for every child and in the case of this Rx extension, we get n different observables for our n children. What should I do if I want each of those children to comprise an observable array? I.e., first signal comes in and gets mapped into an observable of an array, second comes in and we combine with our first one, etc..

mbalex99 commented 8 years ago

What is your end Observable type? Is it a concatenated Observable<[UserModel]> or Observable<[[UserModel]]>

danieljvdm commented 8 years ago

Observable<[UserModel]>

mbalex99 commented 8 years ago

@danieljvdm Oh wow I swear I responded correctly to this.

your can do this:

let arrayOfObservables : [Observable<User>] = ["max", "john", "adam"].map{ username in 
     return rx_observeUserModel(username)
 }

Then do this:

let combined : Observable<[UserModel]> = Observable.combineLatest(arrayOfObservables, 
{ (values: [UserModel] in return values })

So if any of those Observables in the arrayOfObservables changes, you'll get the combined ones.

Does this help?

danieljvdm commented 8 years ago

Yeah I figured this out awhile ago haha, I should have marked this issue closed. Thanks though!