fixed memory leak

Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
Matt Bruce 2022-08-15 09:03:35 -05:00
parent 1b4d40f67e
commit 92ccbbeceb

View File

@ -34,6 +34,10 @@ extension Bindable {
} }
class TextFieldBindingCell: UIView, Bindable { class TextFieldBindingCell: UIView, Bindable {
deinit {
print("\(Self.self) deinit")
}
//bindable //bindable
var model: User var model: User
var subject: CurrentValueSubject<User, Never>? var subject: CurrentValueSubject<User, Never>?
@ -77,19 +81,21 @@ class TextFieldBindingCell: UIView, Bindable {
//add the logic //add the logic
let firstNameSubject = CurrentValueSubject<String, Never>(model.firstName) let firstNameSubject = CurrentValueSubject<String, Never>(model.firstName)
firstNameTextField.createBinding(with: firstNameSubject, storeIn: &subscriptions) firstNameTextField.createBinding(with: firstNameSubject, storeIn: &subscriptions)
firstNameSubject.assign(to: \.model.firstName, on: self).store(in: &subscriptions) firstNameSubject
firstNameSubject.sink {[weak self] value in .sink {[weak self] value in
self?.send() guard let self = self else { return }
self.model.firstName = value
self.send()
}.store(in: &subscriptions) }.store(in: &subscriptions)
let lastNameSubject = CurrentValueSubject<String, Never>(model.lastName) let lastNameSubject = CurrentValueSubject<String, Never>(model.lastName)
lastNameTextField.createBinding(with: lastNameSubject, storeIn: &subscriptions) lastNameTextField.createBinding(with: lastNameSubject, storeIn: &subscriptions)
lastNameSubject.assign(to: \.model.lastName, on: self).store(in: &subscriptions) lastNameSubject
lastNameSubject.sink {[weak self] value in .sink {[weak self] value in
guard let self = self else { return } guard let self = self else { return }
self.model.lastName = value
self.send() self.send()
}.store(in: &subscriptions) }.store(in: &subscriptions)
} }
} }
@ -111,7 +117,11 @@ class TextFieldBindingCell: UIView, Bindable {
view.backgroundColor = .white view.backgroundColor = .white
let subject = CurrentValueSubject<User, Never>(user) let subject = CurrentValueSubject<User, Never>(user)
subject.assign(to: \.user, on: self).store(in: &subscriptions) subject
.sink{ [weak self] updatedModel in
self?.user = updatedModel
}
.store(in: &subscriptions)
let nameTextField = TextFieldBindingCell(model: user) let nameTextField = TextFieldBindingCell(model: user)
nameTextField.createBinding(with: subject, storeIn: &subscriptions) nameTextField.createBinding(with: subject, storeIn: &subscriptions)