Skip to content

Instantly share code, notes, and snippets.

@lizixroy
Created May 30, 2015 00:06
Show Gist options
  • Save lizixroy/f238cc4cc4ece3dac80e to your computer and use it in GitHub Desktop.
Save lizixroy/f238cc4cc4ece3dac80e to your computer and use it in GitHub Desktop.
Interactively explore the strong reference cycle.
/* Let's explore a little bit how to break the strong reference cycles.
* In this Playground file I re-created the examples mentioned in the ARC chapter in the Swift Porgramming Lang book: https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/AutomaticReferenceCounting.html
* In each section, the code is implemented to avoid strong reference cycle. If you want to see how a strong reference cycle can actually occur follow the comments in each section play for yourself.
* Avoiding memory leak is important for making your app memory-efficient and, in some cases, removing the risks of crashing out.
*/
import UIKit
/* Section 1 */
//Now we have a Person class and a Apartment class. A person does not necessarily need an aparment and a aparment does not necessarily need a tenant either.
class Person {
let name: String
init(name: String) {self.name = name}
var aparment:Apartment?
deinit {println("\(name) is being deinitialized")}
}
class Apartment {
let number: Int
init(number: Int) {self.number = number}
// if you uncomment the line below and uncomment the line after that, you will see a strong reference cycle appears
weak var tenant:Person?
// var tenant:Person?
deinit {println("\(number) is being deinitialized")}
}
var person:Person?
var apt:Apartment?
person = Person(name: "Roy Li")
apt = Apartment(number: 1)
person!.aparment = apt
apt!.tenant = person
person = nil
apt = nil
/* Section 2 */
//Now we have a Customer class and a Creditcard class. A Customer does not necessarily have a credicard but a card must belong to a customer
class Customer {
let name:String
init(name:String) {self.name = name}
var creditCard:Creditcard?
deinit{println("\(self.name) is being deinitialized")}
}
class Creditcard {
let cardnumber:Int
// if you uncomment the line below and uncomment the line after that, you will see a strong reference cycle appears
unowned let customer:Customer
// let customer: Customer
init(cardnumber:Int, customer:Customer) {
self.cardnumber = cardnumber
self.customer = customer
}
deinit{println("\(self.cardnumber) is being deinitialized")}
}
var customer:Customer?
var creditcard:Creditcard?
customer = Customer(name: "Roy Li")
creditcard = Creditcard(cardnumber: 102, customer: customer!)
customer!.creditCard = creditcard
customer = nil
creditcard = nil
/* Section 3 */
//Now we have a Country class and a City class. A Country must have a capital city and a city must belong to a country.
class Country {
let name: String
var capitalCity: City!
init(name: String, capitalName: String) {
self.name = name
self.capitalCity = City(name: capitalName, country: self)
}
deinit { println("\(name) is being deinitialized")}
}
class City {
let name: String
// if you uncomment the line below and uncomment the line after that, you will see a strong reference cycle appears
unowned let country: Country
//let country: Country
init(name: String, country: Country) {
self.name = name
self.country = country
}
deinit { println("\(name) is being deinitialized") }
}
var country:Country?
var city:City?
country = Country(name: "China", capitalName: "Bejing")
country = nil
@lizixroy
Copy link
Author

Copy and paste this code in your a playground file. Follow the instructions to see how a strong reference will be created.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment