【问题标题】:RxSwift Driver weakly subscription?RxSwift Driver 弱订阅?
【发布时间】:2018-08-03 18:21:48
【问题描述】:
我正在寻找一种更简单的方法将函数作为参数传递给 Observable 的 subscribe 方法,而不会导致保留循环;我不想每次都必须使用[weak self]。我刚刚遇到这个answer,这正是我想要的。更好的是,它已经被合并到框架本身中。但是,我似乎没有为Driver找到类似的方法。
它已经实施了吗,我只是看错地方了吗?如果没有,你能指导我如何添加它吗? ...作为旁注,您通常如何处理所有[weak self] 无处不在?
【问题讨论】:
标签:
ios
swift
rx-swift
rx-cocoa
【解决方案1】:
顺便说一句,您通常如何处理所有的[weak self]?
我很少使用subscribe,所以我不需要经常使用[weak self]。尽量使用bind(to:),不用担心自己。另一种选择是进行本地参考并使用它。例如:
let foo = self.foo
myObservable.subscribe(onNext: {
// Use of 'foo' here will refer to the local foo, not self.foo.
// Of course if foo is a class type, they will both refer to the
// same object so that's fine.
// No weak self necessary because self isn't part of the chain.
})