Skip to content

Instantly share code, notes, and snippets.

@CodeEagle
Created April 29, 2015 01:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CodeEagle/8d3c681d979d1467f9c4 to your computer and use it in GitHub Desktop.
Save CodeEagle/8d3c681d979d1467f9c4 to your computer and use it in GitHub Desktop.
ARC Retain Cycle Test
class TestObj:UIView {
var done:(()->())!
deinit {
println("deinit TestObj")
}
func doNothing2(){
}
func doNothing(){
self.doNothing2()
}
func goAnimate(){
weak var wself:TestObj! = self
UIView.animateWithDuration(0.2, animations: {() -> Void in
self.frame = CGRectMake(0, 0, 10, 10)
})
println("done")
dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), { () -> Void in
self.doNothing()
})
var doneBlock:()->() = {[weak self] in
self?.doNothing2()
}
self.done = doneBlock
doneBlock()
}
}
var test:TestObj! = TestObj()
test.goAnimate()
test = nil
上面只有doneBlock 在 self.done = doneBlock 之后,自己持有了doneblock 的对象之后,才会有 retain cycle。
如果没有 self.done = doneBlock
var doneBlock:()->() = { in
self.doNothing2()
}
doneBlock()
也不会发生 retain cycle
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment