Last active
June 2, 2020 17:39
-
-
Save ronanrodrigo/1c83741877124e440773dcf842b2653a to your computer and use it in GitHub Desktop.
Different way to test reference cycle
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import XCTest | |
final class Person { | |
var dogs: [Dog] = [] { | |
didSet { dogs.forEach { $0.onBark = { [weak self] in self?.feed() } } } | |
} | |
func feed() { } | |
} | |
final class Dog { | |
weak var owner: Person? | |
var onBark: (() -> Void)? | |
} | |
final class PersonAndDogsTests: XCTestCase { | |
func testWriteYourTestsHere() { | |
var person = Person() as Optional | |
let dog = Dog() | |
dog.owner = person | |
person?.dogs.append(dog) | |
person = nil | |
XCTAssertNil(dog.owner) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import XCTest | |
final class Person { | |
var dogs: [Dog] = [] { | |
didSet { dogs.forEach { $0.onBark = { [weak self] in self?.feed() } } } | |
} | |
func feed() { } | |
} | |
final class Dog { | |
weak var owner: Person? | |
var onBark: (() -> Void)? | |
} | |
final class PersonAndDogsTests: XCTestCase { | |
func testWriteYourTestsHere() { | |
var person = Person() | |
let dog = Dog() | |
dog.owner = person | |
person.dogs.append(dog) | |
XCTAssertTrue(isKnownUniquelyReferenced(&person)) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import XCTest | |
final class Person { | |
var dogs: [Dog] = [] { | |
didSet { | |
dogs.forEach { $0.onBark = weakSelfMethod(self, Person.feed) } | |
} | |
} | |
func feed() { } | |
} | |
final class Dog { | |
var owner: Person? | |
var onBark: (() -> Void)? | |
} | |
func weakSelfMethod<T: AnyObject>(_ valueForSelf: T, _ unbindedMethod: @escaping (T) -> () -> Void) -> (() -> Void) { | |
return { [weak valueForSelf] in | |
if let valueForSelf = valueForSelf { | |
unbindedMethod(valueForSelf)() | |
} | |
} | |
} | |
final class PersonAndDogsTests: XCTestCase { | |
func testWriteYourTestsHere() { | |
var dog: Dog? = Dog() | |
var person: Person? = Person() | |
weak var weakPerson = person | |
weak var weakDog = dog | |
person?.dogs = [dog!] | |
dog?.onBark?() | |
person = nil | |
dog = nil | |
XCTAssertNil(weakPerson) | |
XCTAssertNil(weakDog) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import XCTest | |
final class Person { | |
var dogs: [WeakRef<Dog>] = [] { | |
didSet { dogs.forEach { $0.value?.onBark = feed } } | |
} | |
func feed() { } | |
} | |
final class Dog { | |
weak var owner: Person? | |
var onBark: (() -> Void)? | |
} | |
class WeakRef<T> where T: AnyObject { | |
private(set) weak var value: T? | |
init(value: T?) { | |
self.value = value | |
} | |
} | |
final class PersonAndDogsTests: XCTestCase { | |
func testWriteYourTestsHere() { | |
var dog: Dog? = Dog() | |
var person: Person? = Person() | |
weak var weakPerson = person | |
weak var weakDog = dog | |
person?.dogs = [WeakRef<Dog>(value: dog!)] | |
dog?.onBark?() | |
person = nil | |
dog = nil | |
XCTAssertNil(weakPerson) | |
XCTAssertNil(weakDog) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment