Skip to content

Instantly share code, notes, and snippets.

@DeveloperMaris
Last active January 13, 2022 08:37
Show Gist options
  • Save DeveloperMaris/3e1868f6c430232661604dc62ef6c50b to your computer and use it in GitHub Desktop.
Save DeveloperMaris/3e1868f6c430232661604dc62ef6c50b to your computer and use it in GitHub Desktop.
experiment-weak-or-strong-self-capture
//
// PlayerViewController.swift
// Player
//
// Created by Maris Lagzdins on 12/01/2022.
//
import UIKit
class PlayerViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let player = Player()
player.onReadyToPlay = {
print("Player is ready to play.")
}
player.download()
print("Player view did load.")
}
}
struct DataLoader {
func asyncLoad(closure: @escaping (Data) -> Void) {
// asynchronously loads data.
}
}
class Player {
private let loader = DataLoader()
private let queue = DispatchQueue(label: "Player.Process.Queue")
var onReadyToPlay: (() -> Void)?
func play() {
print("Play player.")
}
func download() {
queue.async { [weak self] in
guard let self = self else { return }
print("Download the audio.")
self.loader.asyncLoad { data in
print("Prepare the audio for playback.")
self.onReadyToPlay?()
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment