-
Notifications
You must be signed in to change notification settings - Fork 70
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allowing set on disposeBag #46
Comments
Hey! Yeah that makes sense. Can you update to the latest NSObject-Rx? The property is currently read/write so this might just work. Otherwise, totally welcome to a PR 😄 |
FYI, I was using the latest version, and the set still failed with the error above. I am unsure why it fails for me but no in your unit tests. I am using Xcode 9 with Swift set to 3.2 Making the change to the protocol has side effects; that is, it would then only work with class types, not value types. I am unsure if you want to accept that limitation or not. Here is a good article that goes into some detail on this issue. |
I see. This case is actually covered by our tests: NSObject-Rx/Demo/DemoTests/DemoTests.swift Lines 8 to 13 in 58cd60b
But I noticed that the Lines 13 to 16 in 58cd60b
Can you try using the following implementation in your cell subclass? override func prepareForReuse() {
super.prepareForReuse()
var mutatingSelf = self
mutatingSelf.rx.disposeBag = DisposeBag()
} |
(Sidenote below.) I though I remembered solving a similar problem, of reusing cells getting rid of their subscriptions. I used the ... whenever the Both approaches have their merits – if I can clarify anything in our implementation, let me know 👍 |
@ashfurrow I just took a look at this new way.. can you elaborate on the advantages of your style? it looks pretty awesome to me.. I've been doing it like this private var bag = DisposeBag()
override func prepareForReuse() {
super.prepareForReuse()
bag = DisposeBag()
} |
I like how explicit it is, and how it removes an aspect of state from my cell class. It's largely a personal preference – either is fine, though in your code I'd make sure to call |
This is my solution. override func prepareForReuse() {
super.prepareForReuse()
rx.clearDisposeBag()
} with public extension Reactive where Base: AnyObject {
public func clearDisposeBag() {
synchronizedBag {
objc_setAssociatedObject(base, &disposeBagContext, nil, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
}
}
} |
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
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:
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
The text was updated successfully, but these errors were encountered: