【问题标题】:Clean VIP with Delegate Pattern具有委托模式的清洁 VIP
【发布时间】:2021-02-22 18:27:15
【问题描述】:

我是 Clean VIP 架构的新手,我正在努力解决它的切入点。

(我只是放了一些代码)

视图控制器

protocol Delegate: class { 
   func execute()
}

class TitlesViewController:UIViewController {
   weak var delegate: Delegate?

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

}

配置器

class TitlesConfigurator {

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

在 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
}

现在我面临的问题是在TilesConfigurator 之外没有interactor 的引用,而delegateweak,这意味着它的总arc 为0。它导致delegate = nil 在@ 内987654330@

如何在我的架构中改进或解决此问题。

P.S:我认为在 ViewController 内部对委托进行强引用并不是一个好习惯

【问题讨论】:

    标签: ios swift clean-architecture viper-architecture


    【解决方案1】:

    这里的代表不应该是weak

    var delegate: Delegate?
    

    因为有一部分是weak,即let interactor = Interaction(),所以不会发生保留周期

    【讨论】:

    • 这是错误的。我已经更新了。但它并没有解决arc的问题
    • 我知道如果我不让代理保持弱点,问题就会得到解决。但一般的说法是代表应该很弱,因为它会导致保留周期。有什么方法可以证明在这种情况下委托不应该是弱的?
    • let interactor = Interaction() 很弱,因为它是一个局部变量,并在 vc 中检查跟踪 deinit 调用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-20
    • 2017-08-01
    • 1970-01-01
    相关资源
    最近更新 更多