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

Triggering Action #41

Closed
asyl opened this issue Aug 27, 2017 · 5 comments
Closed

Triggering Action #41

asyl opened this issue Aug 27, 2017 · 5 comments

Comments

@asyl
Copy link

asyl commented Aug 27, 2017

Is there any chance to trigger the Action without subscribing to State?

For Example:
I have Action saveKey(Key)

enum Action {
  case saveKey(Key)
}

and I want to trigger action case without returning any Mutation case.

func mutate(action: Action) -> Observable<Mutation> {
  switch action {
  case let .saveKey(key):
    userService.appendKey(key)
    return .empty()
  }
}

Should I return .empty() observable? But it doesn't work

@asyl asyl changed the title triggering Action Triggering Action Aug 27, 2017
@zdnk
Copy link

zdnk commented Aug 27, 2017

you need to subscribe to it so it is not disposed/deallocated right away.

@asyl
Copy link
Author

asyl commented Aug 27, 2017

@zdenektopic you mean it needs to subscribe to Action?

@devxoul
Copy link
Member

devxoul commented Aug 28, 2017

@asyl,

  1. I didn't understand 'triggering the Action without subscribing to State'. Could you please share me some detailed situation?

  2. You should always return Observable from mutate(). Returning .empty() is also my preferred way but it would be better to use like:

    return Observable.create { [weak self] in
      self?.userService.appendKey(key)
      return Disposables.create()
    }

@asyl
Copy link
Author

asyl commented Aug 28, 2017

@devxoul Thank you.

Scenario

I used ReactorKit and realized that my actions really work when I subscribe to the State.
For example:
I have UI components bound to some action in my Reactor

self.button.rx.tap
  .flatMap { self.textField.rx.text }
  .map { Reactor.Action.appendElement($0) }
  .bind(to: reactor.action)
  .disposed(by: disposeBag)

where Action:

enum Action {
  case appendElement(String)
}

and in mutate(action: Action) function, I have only one Side Effect where I save my element into the disk. But it doesn't work. Even break points don't work unless I create Mutation and State for this case and I subscribe to the State in my View, like this:

reactor.state.map { $0.element }
  .subscribe { print($0) }
  .disposed(by: disposeBag)

After all of that, I can see how break points stop in mutate(action: Action) and function works.

Question

How can I bind my UI components toAction without creating any Mutation & State?

@devxoul
Copy link
Member

devxoul commented Aug 28, 2017

Ah, I got it. You may want to create a state stream without binding it to the view. Try this:

class MyReactor {
  init(params: MyParameters) {
    self.initialState = State(foo: bar)
    _ = self.state // add this line to create a state immediately
  }
}

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

3 participants