-
Notifications
You must be signed in to change notification settings - Fork 68
Description
I ran into an issue where we are subscribing to changes of a UITextField in a UITableViewCell instance. Because the cells can be re-used, we should dispose in the prepareForReuse - we cannot wait for a deinit. Otherwise, we end up with multiple subscribers on a UITextField which really confuses things.
I first attempted to implement via a
override func prepareForReuse() {
super.prepareForReuse()
disposeBag = DisposeBag()
}
But then we get the Swift error "Cannot assign to property: 'self' is immutable". There are a number of articles about this issue, and from what I have seen, the simplest and cleanest solution is to modify the HasDisposeBag protocol like so:
public protocol HasDisposeBag: class {
/// a unique Rx DisposeBag instance
var disposeBag: DisposeBag { get set }
}
This allows the setting of the disposeBag within the UITableViewCell and thus keep the subscribers to the correct set.
There may be other solutions available, but if this is a good solution, it would be great to see it changed in the original source (I am currently using a fork I made with just this one change).
Thank you