Skip to content
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

Unsubscribing and timer #67

Closed
Odrakir opened this issue Jul 24, 2015 · 6 comments
Closed

Unsubscribing and timer #67

Odrakir opened this issue Jul 24, 2015 · 6 comments
Labels

Comments

@Odrakir
Copy link

Odrakir commented Jul 24, 2015

I'm trying to translate to RxSwift an example in the "Intro to Rx" web.(http://www.introtorx.com/Content/v1.0.10621.0/04_CreatingObservableSequences.html down where it creates a Timer)

This is what I have:

    var ob:Observable<String> = create {
        observer in

        var timer = NSTimer.schedule(repeatInterval: 1.0) { timer in
            sendNext(observer, "value")
            println("timer event \(timer.fireDate)")
        }

        return AnonymousDisposable {
           // timer.invalidate()
        }
    }

    var subscription = ob
        >- subscribeNext {
            e in
            println(e)
    }

    var dispatchTime: dispatch_time_t = dispatch_time(DISPATCH_TIME_NOW, Int64(3 * Double(NSEC_PER_SEC)))
    dispatch_after(dispatchTime, dispatch_get_main_queue(), {
        println("unsubscribe \(subscription)")
        subscription.dispose()
    })

I'm trying to unsubscribe while keeping the timer running (as the example does) and then invalidate the timer in the dispose block (by uncommenting the "timer.invalidate()" line) to demonstrate the use of the disposable for cleaning purposes.

The problem is, with the code as it is, after the 3 seconds passed, the timer is still running (as expected) but the subscription seems to be active as well.

This is what I get:

value
timer event 2015-07-24 07:43:51 +0000
value
timer event 2015-07-24 07:43:52 +0000
value
timer event 2015-07-24 07:43:53 +0000
unsubscribe RxSwift.AnonymousDisposable
value
timer event 2015-07-24 07:43:54 +0000
value
timer event 2015-07-24 07:43:55 +0000

If I invalidate the timer, of course, I stop receiving events, but it seems I'm not unsubscribing for some reason.

@Odrakir
Copy link
Author

Odrakir commented Jul 24, 2015

By the way, I'm using this gist for the timer: https://gist.github.com/natecook1000/b0285b518576b22c4dc8

@kzaher kzaher added the bug label Jul 24, 2015
@kzaher
Copy link
Member

kzaher commented Jul 24, 2015

Hi,
wow :) It's a bug. Thnx for creating an issue.

So AutoDetach observer was in place and it was disposing in case of sequence was completed, but in case you didn't dispose producer, it would pass events until producer was disposed.

Problem was here

override public func subscribe<O : ObserverType where O.Element == Element>(observer: O) -> Disposable {
        let autoDetachObserver = AutoDetachObserver(observer: observer)

        let disposable = subscribeCore(ObserverOf(autoDetachObserver))
        autoDetachObserver.setDisposable(disposable)

        return autoDetachObserver //<< was disposable 
    }

I can fix this, add additional unit tests and push this to develop branch.

This doesn't seem to me like a critical issue, so are you ok just using develop branch code until next release until this enters master branch?

@Odrakir
Copy link
Author

Odrakir commented Jul 24, 2015

Hi, thanks for the fast response.

It's ok, I'm just learning, so I'm not in a hurry. It just helps to know it wasn't me doing something wrong. I'll keep an eye on the fix to see if I can understand what was going on :)

@alcarvalho
Copy link

I think I am experiencing this bug, too... but with throttle.

@kzaher
Copy link
Member

kzaher commented Jul 24, 2015

The fix should be in develop

commit 24dc2c6
Author: Krunoslav Zaher krunoslav.zaher@gmail.com
Date: Fri Jul 24 16:47:49 2015 +0200

Fixes problem with issues/67.

@alcarvalho send me all heisenbugs 😛

@kzaher
Copy link
Member

kzaher commented Aug 3, 2015

These changes have been released in the latest release. Closing this issue. Let me know if it happens again.

@kzaher kzaher closed this as completed Aug 3, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants