Can not send data to another module in VIPER
Asked Answered
K

3

12

How can i send data from Module A to Module B in VIPER? I use router A, which has information for Module B, and try to send this information to view controller B or presenter B. What is the best way to do it?

Kern answered 18/7, 2016 at 8:4 Comment(1)
Have you read the "Book of VIPER" by Rambler?Causal
H
7

In that case my workflow is:

  1. Usually the user interface (view) in module A launches an event that triggers the module B.
  2. The event reaches the presenter in module A. The presenter knows it has to change module and notifies wireframe who knows how to make the change.
  3. The wireframe in module A notifies to wireframe in module B. In this call sends all data needed
  4. The wireframe in module B continues its normal execution, transferring the information to the presenter

wireframe in module A must know wireframe B

Hum answered 27/7, 2016 at 7:25 Comment(0)
D
5

Use delegates to send data between VIPER modules:

// 1. Declare which messages can be sent to the delegate

// ProductScreenDelegate.swift
protocol ProductScreenDelegate {
//Add arguments if you need to send some information
    func onProductScreenDismissed()
    func onProductSelected(_ product: Product?)
}

// 2. Call the delegate when you need to send him a message

// ProductPresenter.swift
class ProductPresenter {

    // MARK: Properties
    weak var view: ProductView?
    var router: ProductWireframe?
    var interactor: ProductUseCase?
    var delegate: ProductScreenDelegate?
}

extension ProductPresenter: ProductPresentation {

    //View tells Presenter that view disappeared
    func onViewDidDisappear() {

        //Presenter tells its delegate that the screen was dismissed
        delegate?.onProductScreenDismissed()
    }
}

// 3. Implement the delegate protocol to do something when you receive the message

// ScannerPresenter.swift
class ScannerPresenter: ProductScreenDelegate {

    //Presenter receives the message from the sender
    func onProductScreenDismissed() {

        //Presenter tells view what to do once product screen was dismissed
        view?.startScanning()
    }
    ...
}

// 4. Link the delegate from the Product presenter in order to proper initialize it

// File ScannerRouter.swift
class ProductRouter {

    static func setupModule(delegate: ProductScreenDelegate?) -> ProductViewController {
        ...
        let presenter = ScannerPresenter()

        presenter.view = view
        presenter.interactor = interactor
        presenter.router = router
        presenter.delegate = delegate // Add this line to link the delegate
        ...
        }
}

For more tips, check this post https://www.ckl.io/blog/best-practices-viper-architecture/

Denunciate answered 11/4, 2017 at 2:0 Comment(0)
K
3

Does wireframe have reference to presenter? This version of VIPER which i use

The router knows about another module and tells view to open it. Assembly combines all parts of module.

Kern answered 28/7, 2016 at 6:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.