ReactiveCocoa MutableProperty绑定创建保留周期,泄漏内存

时间:2017-08-25 14:01:37

标签: ios swift reactive-cocoa reactive-swift reactive-cocoa-5

我有一个简单的UIViewViewController应用,带有UILabel个插座。我有一个MutableProperty<String?>我绑定到label.reactive.text这会导致57个内存泄漏。绑定两个可变属性是否总是泄漏内存,如果是这样,为什么?

class ViewController: UIViewController {

    @IBOutlet weak var testLabel: UILabel!
    let testText: MutableProperty<String?> = MutableProperty("")

    override func viewDidLoad() {
        super.viewDidLoad()
        let signal = textSignal()
        testText <~ signal
        testLabel.reactive.text <~ testText

        DispatchQueue.main.asyncAfter(deadline: .now() + 6) {
            UIApplication.shared.keyWindow?.rootViewController = UIViewController()
        }
    }

    func textSignal() -> SignalProducer<String, NoError> {
        return SignalProducer<String, NoError>{ (sink: Observer, disposable: Disposable) in
            DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
                sink.send(value: "Hello world")
                print("sink Hello world")
            }
            DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
                sink.send(value: "Wabbu Labbu dub dub")
                print("sink Wabbu Labbu dub dub")
            }
            DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
                sink.sendCompleted()
                print("sink compeleted")
            }
        }
    }
}

0 个答案:

没有答案