我已经使用ReactiveSwift几个月了,但是有一些我不完全了解的内容:生存期对象。
例如,假设我有一个SignalProducer将进行API调用的函数,它包装在一个类上:
class ServiceWrapped {
private let service: Service // the method called on this object returns the SignalProducer
private let (lifetime, token) = Lifetime.make()
// more stuff
func fetchSomething(completion: @escaping (Value?, Error?) -> Void) {
lifetime += service.fetchSomething()
.startWithResult { result in
switch result {
case .success(let value):
completion(value, nil)
case .failure(let error):
completion(nil, error)
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是:是否有必要lifetime在这种情况下使用?
我知道lifetime它将保留服务调用,以便它在返回时具有某些内容,但是由于这也被包装了,ServiceWrapped所以我认为使用并不是lifetime真正必要。
提前致谢。