From dcdb31360fe32d9daef31e31c610e8a435af36fb Mon Sep 17 00:00:00 2001 From: Erik Terwan Date: Tue, 29 Mar 2022 20:43:16 +0200 Subject: [PATCH] Use weak self reference in .sink instead of force casting --- MMMLoadable.podspec | 2 +- Sources/MMMLoadable/MMMLoadable.swift | 9 +++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/MMMLoadable.podspec b/MMMLoadable.podspec index 109a981..b84fce9 100644 --- a/MMMLoadable.podspec +++ b/MMMLoadable.podspec @@ -6,7 +6,7 @@ Pod::Spec.new do |s| s.name = "MMMLoadable" - s.version = "1.7.0" + s.version = "1.7.1" s.summary = "A simple model for async calculations" s.description = "#{s.summary}." s.homepage = "https://github.com/mediamonks/#{s.name}" diff --git a/Sources/MMMLoadable/MMMLoadable.swift b/Sources/MMMLoadable/MMMLoadable.swift index e35fedd..c2b83bd 100644 --- a/Sources/MMMLoadable/MMMLoadable.swift +++ b/Sources/MMMLoadable/MMMLoadable.swift @@ -4,6 +4,7 @@ // import Foundation +import MMMCommonCore #if SWIFT_PACKAGE @_exported import MMMLoadableObjC @@ -23,8 +24,12 @@ extension MMMPureLoadableProtocol { /// - Returns: The observer, you usually want to store this outside of the scope, e.g. /// in a private property so it doesn't deallocate right away. public func sink(_ block: @escaping (Self) -> Void) -> MMMLoadableObserver? { - return MMMLoadableObserver(loadable: self) { loadable in - block(loadable as! Self) + return MMMLoadableObserver(loadable: self) { [weak self] _ in + guard let self = self else { + assertionFailure("\(MMMTypeName(Self.self)) was lost inside the observer callback?") + return + } + block(self) } } }