简体   繁体   English

具有委托模式的清洁 VIP

[英]Clean VIP with Delegate Pattern

I am new to Clean VIP Architecture and I am struggling with its entry point.我是 Clean VIP Architecture 的新手,我正在努力解决它的切入点。

(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在 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现在我面临的问题是TilesConfigurator之外没有interactor器的引用,并且delegate weak ,这意味着它的总arc为 0。它导致viewDidLoad内的delegate = nil

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 PS:我不认为在ViewController中对委托进行强引用是一种好的做法

Delegate shouldn't be weak here代表在这里不应该weak

var delegate: Delegate?

As there is one part that's weak which is let interactor = Interaction() so no retain cycles will occur因为有一个部分weak ,它是let interactor = Interaction()所以不会发生保留周期

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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