ReactiveCocoa / ReactiveSwift

Streams of values over time

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Memory leak using SignalProducer.timer(...)

FranDepascuali opened this issue · comments

commented

Description

Hey there! I had a memory leak using timer but I'm not completely sure why.

I have this:

class ToastEventsPresenter {
    fileprivate (set) var _toasts: SignalProducer<[ToastEvent], NoError>!

    init() {
        let sampler = SignalProducer
                                  .timer(interval: DispatchTimeInterval.seconds(2), on: QueueScheduler())
                                  .map { _ in }
                                  .take(duringLifetimeOf: self)

    _toasts = SignalProducer
            .combineLatest(
                sampler,
               ...
            )
            .map { (arg) -> [ToastEvent] in
                ...
            }
}

with that code, just by instantiating a ToastEventsPresenter it never gets deallocated.

I changed the code to:

class ToastEventsPresenter {
    fileprivate (set) var _toasts: SignalProducer<[ToastEvent], NoError>!

    init() {
        let sampler = SignalProducer
                                  .timer(interval: DispatchTimeInterval.seconds(2), on: QueueScheduler())
                                  .map { _ in }
                                  .take(duringLifetimeOf: self)
        let _sampler = ReactiveSwift.Property<()>(initial: (), then: sampler)
        _toasts = SignalProducer
            .combineLatest(
                _sampler,
               ...
            )
            .map { (arg) -> [ToastEvent] in
                ...
            }
}

and it works correctly.

Any ideas of why this happens?

Hello. 👋 Thanks for opening this issue. Due to inactivity, we will soft close the issue. If you feel that it should remain open, please let us know. 😄

commented

No problem, thanks :)