简体   繁体   中英

Clean VIP with Delegate Pattern

I am new to Clean VIP Architecture and I am struggling with its entry point.

(I am only putting some bit of code)

ViewController

protocol Delegate: class { 
   func execute()
}

class TitlesViewController:UIViewController {
   weak var delegate: Delegate?

   func viewDidLoad() {
         super.viewDidLoad()
         delegate.execute()
      }

}

Configurator

class TitlesConfigurator {

static func configureModule(viewController: TitlesViewController) {
    let interactor = Interaction()
    
    viewController.delegate = interactor
    
   }
}

In AppDelegate

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    
    let titlesViewController = TitlesViewController()
    let navigationController = UINavigationController(rootViewController: titlesViewController)
    TitlesConfigurator.configureModule(viewController: titlesViewController)
    
    window = UIWindow()
    window?.rootViewController = navigationController
    window?.makeKeyAndVisible()
    
    return true
}

Now Issue that I am facing is that there is no reference of interactor outside of TilesConfigurator and delegate is weak which means its total arc is 0. It results in delegate = nil inside viewDidLoad

How can I improve or fix this issue in my architecture.

PS: I don't think its good practice to make a strong reference of delegate inside ViewController

Delegate shouldn't be weak here

var delegate: Delegate?

As there is one part that's weak which is let interactor = Interaction() so no retain cycles will occur

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM