Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creating a CocoaAction correctly #26

Closed
morgz opened this issue Feb 16, 2016 · 7 comments
Closed

Creating a CocoaAction correctly #26

morgz opened this issue Feb 16, 2016 · 7 comments

Comments

@morgz
Copy link

morgz commented Feb 16, 2016

Hey,

Thanks for the library! I think I'm starting to understand it. I was using rx_tap on a button but have since switched to trying CocoaAction for the enabling/disabling functionality.

Can you just quickly sense check a few of my understandings? I have the following block of code that is just an Action. When it's invoked, it completes after a 4.5 sec delay.

let newAction = CocoaAction {

    let requestObservable: Observable<Bool> = Observable.create { (observer) -> Disposable in

        //Pretend we're doing a network request

        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(4.5 * Double(NSEC_PER_SEC))), dispatch_get_main_queue()) {
            observer.onNext(true)
            observer.on(.Completed)
        }

        return AnonymousDisposable{}
    }

    return requestObservable.flatMap{_ in
        // Pretend I do something useful with my result from the first Observable.
        // Now CocoaAction is expecting a Observable<Void,Void> return value so I...

        return Observable.empty()
    }
}
  1. I can't just return requestObservable . I have to return an Observable<Void, Void>
  2. My inner calls should not end in a .subscribe as this would not return an Observable. .rx_action is expecting an Observable so it can use.bindTo(self.x_enabled) etc.

Does that sound about right?

@ashfurrow
Copy link
Member

Hey there! OK, a few things, and these should be added to our documentation because you questions are really common.

  1. Sort of, I'll explain below.
  2. Definitely not end in .subscribe, you're correct.

OK so you can map any observable to be an Observable<Void> with map:

observable.map { _ in Void() }

I do this so often I have a function to do it for me:

observable.map(void)

I wouldn't recommend dispatch_after, but I think you just have that in there for testing, right? Otherwise, you could use RxSwift for that:

Observable<Int>.interval(4.5, scheduler: MainScheduler.instance)

OK, so I would rewrite things like this:

let newAction = CocoaAction {

    let requestObservable: Observable<Bool> = Observable<Int>.interval(self.pollInterval, scheduler: MainScheduler.instance).map { _ in return true } // Just for testing

    return requestObservable.flatMap { resultFromFirstObservable in
        // Pretend I do something useful with my result from the first Observable.
        // Maybe subscribe to another observable to do some work or something.
    }.map{ _ in return Void() } // Makes an Observable<Void> so the type matches with CocoaAction.
}

Let me know if that makes sense! Happy to clarify further.

@morgz
Copy link
Author

morgz commented Feb 16, 2016

Thanks Ash, that clears things up. I'll add a few notes to the documentation issue.

@ashfurrow
Copy link
Member

Thanks! That would be great 🙇

@werediver
Copy link

@ashfurrow We have a link to this question in #27, and I think that's enough to keep track of necessary documentation improvements.

Let's close this ticket.

@ashfurrow
Copy link
Member

Makes sense to me. @morgz sound okay to you?

@freak4pc
Copy link
Member

Hey @morgz , Since it's been two months with no response and it seems like your question was answered, I'm closing this for now.

If you need any more help feel free to reopen or comment here !

Cheers :)

@morgz
Copy link
Author

morgz commented May 28, 2017

Yes, sorry for the lack of response. Thanks guys, appreciate the hard work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants