Skip to content
This repository has been archived by the owner on Jan 2, 2024. It is now read-only.

Commit

Permalink
Fix memory leaks
Browse files Browse the repository at this point in the history
  • Loading branch information
tonisevener committed Apr 5, 2023
1 parent c7b8e77 commit fc5c7a9
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 9 deletions.
Expand Up @@ -39,7 +39,7 @@ open class WKComponentHostingController<HostedView: View>: UIHostingController<H

private func subscribeToAppEnvironmentChanges() {
WKAppEnvironment.publisher
.sink(receiveValue: { _ in self.appEnvironmentDidChange() })
.sink(receiveValue: { [weak self] _ in self?.appEnvironmentDidChange() })
.store(in: &cancellables)
}

Expand Down
Expand Up @@ -36,7 +36,7 @@ public class WKComponentView: UIView {

private func subscribeToAppEnvironmentChanges() {
WKAppEnvironment.publisher
.sink(receiveValue: { _ in self.appEnvironmentDidChange() })
.sink(receiveValue: { [weak self] _ in self?.appEnvironmentDidChange() })
.store(in: &cancellables)
}

Expand Down
Expand Up @@ -34,7 +34,7 @@ open class WKComponentViewController: UIViewController {

private func subscribeToAppEnvironmentChanges() {
WKAppEnvironment.publisher
.sink(receiveValue: { _ in self.appEnvironmentDidChange() })
.sink(receiveValue: { [weak self] _ in self?.appEnvironmentDidChange() })
.store(in: &cancellables)
}

Expand Down
Expand Up @@ -35,10 +35,16 @@ public class WKRandomPhotoViewController: WKComponentViewController {

private func loadImage() {
customView.activityIndicator.startAnimating()
viewModel.loadImage(completion: { image in
let configuration = viewModel.configuration
viewModel.loadImage(completion: { [weak self] image in
DispatchQueue.main.async {

guard let self = self else {
return
}

self.customView.activityIndicator.stopAnimating()
self.customView.update(configuration: self.viewModel.configuration, image: image)
self.customView.update(configuration: configuration, image: image)
}
})
}
Expand Down
Expand Up @@ -39,17 +39,17 @@ public class WKRandomPhotoViewModel {
public func loadImage(completion: @escaping (UIImage) -> Void) {
switch configuration {
case .cats:
let task = URLSession.shared.dataTask(with: Configuration.cats.api) { data, _, _ in
let task = URLSession.shared.dataTask(with: Configuration.cats.api) { [weak self] data, _, _ in

if let data = data, let cat = try? JSONDecoder().decode(Cat.self, from: data) {
self.fetchPhoto(cat.file, completion: completion)
self?.fetchPhoto(cat.file, completion: completion)
}
}
task.resume()
case .dogs:
let task = URLSession.shared.dataTask(with: Configuration.dogs.api) { data, _, _ in
let task = URLSession.shared.dataTask(with: Configuration.dogs.api) { [weak self] data, _, _ in
if let data = data, let dog = try? JSONDecoder().decode(Dog.self, from: data) {
self.fetchPhoto(dog.message, completion: completion)
self?.fetchPhoto(dog.message, completion: completion)
}
}
task.resume()
Expand Down

0 comments on commit fc5c7a9

Please sign in to comment.