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

Interactor calls Presentable before view loads [iOS] #222

Closed
sham3k opened this issue Mar 13, 2018 · 14 comments
Closed

Interactor calls Presentable before view loads [iOS] #222

sham3k opened this issue Mar 13, 2018 · 14 comments

Comments

@sham3k
Copy link

sham3k commented Mar 13, 2018

There seems to be an issue where, if the interactor has to update its view in didBecomeActive, the view has not yet loaded when didBecomeActive is called, leading to a crash. Any workaround to wait for the view to load and then update UI?

@sbarow
Copy link
Contributor

sbarow commented Mar 14, 2018

Do you have some example code for this? Can't say I have ever seen this before.

@kronik
Copy link
Contributor

kronik commented Mar 15, 2018

I usually use following pattern:

  1. ViewController in viewDidLoad method calls listener?.didPrepareView
  2. Interactor implements didPrepareView and starts loading data or interact with presenter

@sbarow is there any reason why didBecomeActive is called so early?

@sbarow
Copy link
Contributor

sbarow commented Mar 15, 2018

@neakor any insights into this?

@neakor
Copy link
Contributor

neakor commented Mar 15, 2018

There are a couple of solutions for this.

  1. Given that when and how to display the data the interactor passes to the view controller is view logic, the view controller should take the responsibility to store the data internally. It can then render this data when the appropriate view controller lifecycle is reached, such as viewDidAppear.
  2. Instead of storing the data, the view controller could use lifecycle bound methods such as confineToView(events). Although I'm not sure if we open sourced this.

@sham3k
Copy link
Author

sham3k commented Mar 19, 2018

@kronik Thanks. Am using something similar to listener?.didPrepareView before pushing anything to the view. Expected the tutorial to have something similar to this.

@neakor
Copy link
Contributor

neakor commented Mar 19, 2018

@sham3k does my reply above help clarifying this?

@sham3k
Copy link
Author

sham3k commented Mar 20, 2018

@neakor I felt @kronik has a better solution. Also, I noticed RIBs is currently at version 0.9. Any timeline for the next version or is this prod-ready?

@neakor
Copy link
Contributor

neakor commented Mar 20, 2018

@sham3k it is production ready. This has been our architecture for the Uber app since almost 2 years ago. We are working on the next version to simplify the architecture and reducing boilerplate.

@neakor neakor closed this as completed Mar 20, 2018
@lisacmer
Copy link

I feel like @kronik solution goes in conflict with the whole "business logic drives the app, not the view tree" thing. Considering this, seems like @neakor solution is better (it seems that this is the way this situation is handled in BasicScoreBoardViewController in tutorial) , but it doesn't solve a problem of routing submodule to subview before the main view did load. I was wondering if next version will solve this problem some way or another (maybe by forcing the view to load, which for me seems like an antipattern).

@metaine
Copy link

metaine commented Jun 11, 2020

I wonder if this can be solved by making Presenter a ViewModel, updating VM from Interactor - and subscribing to Presenter (ViewModel) updates in ViewController when VC is actually ready. Or it is just against a RIBs/Clean Architecture way?

@metaine
Copy link

metaine commented Oct 28, 2020

@neakor Could you please go into detail about lifecycle bound events and confineToView method?

For now, all my Presentable methods are writing to (ViewController's) reactive sequences, which are subscribed to in viewDidLoad(), effectively modifying UI at the point when the view is loaded and safe to operate. This is quite verbose, but... well... OK.

Things are getting tricky when there is a need to call not only Presentable but also ViewControllable methods. Like asking router to add child RIBs/subviews inside didBecomeActive(). Do we need a new reactive var (stored state) inside Presentable to add submodules when view is ready? Not nice (and feels like more MVVM/SwiftUI-ish way of building screen hierarchies).

Do we really need didLoadView() in PresentableListener protocol in order to call Presentable/ViewControllable methods from Interactor safely? Does it fit the RIBs control flow paradigm? (my guess it doesn't)

The problem of calling UI methods when the UI is not ready is the only unclear part of overall clean and beautiful framework.

@metaine
Copy link

metaine commented Oct 29, 2020

I ended up with

func add(_ viewControllable: ViewControllable) {
        if isViewLoaded {
            internalAdd(viewControllable)
        } else {
            rx.viewDidLoad.single().subscribe(onNext: { [weak self, weak viewControllable] _ in
                guard let self = self, let viewControllable = viewControllable else { return }
                self.internalAdd(viewControllable)
            }).disposed(by: disposeBag)
        }
    }

@eunjin3786
Copy link

@metaine
Hello 🙂
Where do you call func add(_ viewControllable: ViewControllable) method?
And Could you explain more about internalAdd?

@metaine
Copy link

metaine commented Jul 8, 2021

@metaine
Hello 🙂
Where do you call func add(_ viewControllable: ViewControllable) method?
And Could you explain more about internalAdd?

This one is called from Router. Once Interactor asks Router to add a new child RIB, that RIB is created inside some Router public method by calling [child-RIB-Builder].build(). A (child)ViewControllable (returned as a field of resulting ViewableRouting) then added to Router's ViewControllable. This is very common flow.
internalAdd() is a place where you may call your View related methods safely, knowing for sure that View have actually been loaded.

BTW. Not sure if if isViewLoaded check is needed.

func add(_ viewControllable: ViewControllable) {
        rx.viewDidLoad.single().subscribe(onNext: { [weak self, weak viewControllable] _ in
      --- your code

might be enough

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

7 participants