使用[弱]防止模型中的内存泄漏无法通过swift工作

时间:2017-07-02 04:16:49

标签: ios swift memory-leaks reference

我对内存泄漏有疑问 我想释放记忆,使用[弱自我],但似乎无法释放 如何释放记忆成功?

Memory graph like this.

代码:

class City:NSObject {
    let notifySubject = PublishSubject<Any>()

    var id:String = ""
    @objc var name:String = ""
    var kind:String = ""
    var userId:String = ""
    var country:String = ""
    var subject:String = ""
    var descriptions:String = ""
    var cities:[City] = [City]()

    override init() {
        super.init()
    }

    init(_ id:String) {
        super.init()
        self.id = id
    }

    func getData() {

        API.Data.get({ json in

            json.forEach({ [weak self] (index, data) in
                self?.cities.append(City(data))
            })
            self.fromJson(json)
            self.notifySubject.onNext(json)
        })
    }

}

0 个答案:

没有答案