I'm having a hard time with dealing with Combine. After the publisher is complete I want to update a value but whenever I update that value the memory is allocated and never goes away.
Whenever I try to assign image there is a leak. If I don't assign no leak.
EDIT: Reproducible example here: https://github.com/peterwarbo/MemoryAllocation
This is what my code looks like:
final class CameraController: ObservableObject {
private var storage = Set<AnyCancellable>()
var image: UIImage?
func capture(_ image: UIImage) {
PhotoLibrary.saveImageToTemporaryDirectory(image) // AnyPublisher<URL, Error>
.zip(PhotoLibrary.saveImage(image, location: self.locationObserver.location) // AnyPublisher<UIImage, Error>)
.sink(receiveCompletion: { [weak self] (completion) in
switch completion {
case let .failure(error):
Log.error(error)
self?.handleCaptureError(error)
case .finished: break
}
}) { [weak self] (value) in
print(value.1) // no leak
self.image = value.1 // leak
}
.store(in: &self.storage)
}
}
I've also tried instead of using sink
:
.receive(
subscriber: Subscribers.Sink(
receiveCompletion: { [weak self] completion in
switch completion {
case let .failure(error):
Log.error(error)
self?.handleCaptureError(error)
case .finished: break
}
},
receiveValue: { value in
print(value.1) // no leak
self.image = value.1 // leak
}
)
)