4

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 ?

Borys Verebskyi
  • 4,160
  • 6
  • 28
  • 42
MuraliMohan
  • 1,063
  • 1
  • 11
  • 30

2 Answers2

4
  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
3

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.

Tim007
  • 2,557
  • 1
  • 11
  • 20
  • it wont be released means that wil exist in memory and will form retain cycle ? right ? – MuraliMohan Mar 14 '16 at 09:37
  • 1
    `View` retain `Presenter`, `Presenter` retain `Wireframe` and `Interact`. So if `view` is released -> `Presenter` release -> `Interactor` and `Wireframe` release so it won't be retain recycle. – Tim007 Mar 14 '16 at 09:43
  • Check these for example: `View` https://github.com/ifobos/JRTViperPattern/blob/master/JRTViper/JRTViper/PodFiles/JRTViperViewProtocol.h `Presenter` https://github.com/ifobos/JRTViperPattern/blob/master/JRTViper/JRTViper/PodFiles/JRTViperPresenterProtocol.h `Interactor`https://github.com/ifobos/JRTViperPattern/blob/master/JRTViper/JRTViper/PodFiles/JRTViperInteractorProtocol.h `Router`https://github.com/ifobos/JRTViperPattern/blob/master/JRTViper/JRTViper/PodFiles/JRTViperRouterProtocol.h – Tim007 Mar 14 '16 at 09:47
  • ok understood the flow . can you also answer me why in and out protocols are used for presenter and interactor. cannot we use from presenter -- > interactor using interactor object and from Interactor --> presenter using delegate ? – MuraliMohan Mar 14 '16 at 09:56
  • You can use both ways, protocols let you reuse code without forcing you into a superclass-subclass relationship. Check these designs https://dl.dropboxusercontent.com/u/110377187/viper2.png https://dl.dropboxusercontent.com/u/110377187/viper.png – Tim007 Mar 14 '16 at 10:10
  • in sencond comment even if view is released there is a cycle between prensenter--interactor . so there are retain count 1 on both with. wont it lead to retain cycle . clarify me dude. Thanks .If i follow the diagram flow with strong and week then i am clear.But if i follow https://github.com/objcio/issue-13-viper , then it will lead to cycle right ? – MuraliMohan Mar 23 '16 at 11:07