Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why protocols are used in both direction in VIPER architecture rather than in one direction

We want to change my architecture from MVC to VIPER. I read basic tutorial by following http://mutualmobile.github.io/blog/2013/12/04/viper-introduction/

source code link : https://github.com/mutualmobile/Counter

- (void)createScreen
{
    CNTCountViewController* view = [[CNTCountViewController alloc] init];
    CNTCountPresenter* presenter = [[CNTCountPresenter alloc] init];
    CNTCountInteractor* interactor = [[CNTCountInteractor alloc] init];

    view.presenter = presenter;//object
    presenter.view = view;//protocol

    presenter.interactor = interactor;
    interactor.output = presenter;

    self.window.rootViewController = view;
}

Where for communication from viewcontroller ---> presenter is via preseter's object and presenter --- > viewcontroller throught delegate (protocol). I think this is to avoid retain cycles.

But i also went through one more tutorial https://www.objc.io/issues/13-architecture/viper/ source code link :https://github.com/objcio/issue-13-viper

where he used protocols only for both direction in VTDListWireframe

- (void)presentListInterfaceFromWindow:(UIWindow *)window
{
    VTDListViewController *listViewController = [self listViewControllerFromStoryboard];
    listViewController.eventHandler = self.listPresenter;//protocol
    self.listPresenter.userInterface = listViewController;//protocol
    self.listViewController = listViewController;

    [self.rootWireframe showRootViewController:listViewController
                                      inWindow:window];
}

Here

1)What is the advantage of using protocols in both direction ?

2)I observed that both protocol references are with strong property declaration in both classes.won't it lead to retain cycle ?

like image 479
MuraliMohan Avatar asked Sep 15 '25 02:09

MuraliMohan


2 Answers

  1. The reason he uses delegation in this direction presenter --- > viewcontroller is that Presenter must not know anything about the ViewController. They must be decoupled. Presenter must be independent and thus, reusable. And that delegate var must be weak The one reason why protocols are used in this direction ViewController ---> Presenter is that you can easily extend the functionality of the Presenter by just conforming your Presenter to a new protocol
  2. Yes. For example, @property (nonatomic, strong) UIViewController
like image 57
Yesbol Kulanbekov Avatar answered Sep 16 '25 17:09

Yesbol Kulanbekov


1) If View(UIViewController) is released then whole module will be released.

https://medium.com/mobile-travel-technologies/architecting-mobile-apps-with-b-viper-modules-e94e277c8d68#.e4eh7c1l2

enter image description here

2) in VTDListWireframe, that listViewController is RootViewController so it won't be release so it won't lead to retain recycle. Just read these codes for idea imo.

like image 30
Tim007 Avatar answered Sep 16 '25 16:09

Tim007