automatic ref counting - For what variables should you use weak var in Swift? How do I figure out the leaks? -
i building app in swift. creating views in entirely programmatic way. in instances, have viewcontroller instantiates custom views. in addition, may have variables "var user" gets populated after alamofire network call , used in various ui elements throughout view controller. besides declaring delegates weak var, there other rules apply?
also, there way me figure out whether have strong reference should weak one? should looking for?
the basic concept behind reference counting in swift 1 of ownership. objects should hold strong references other objects "own", in sense they're responsible lifecycle of other object, either alone or in conjunction other objects.
a lot of object reference graphs in typical application hierarchical - 1 object owns bunch of other objects, each have own children, etc. example, viewcontroller owns window, window owns views, each view owns subviews, , each subview owns images, strings, or other content displays. these strong references.
weak references typically used references don't imply ownership. delegate example 1 - in cases, view does not own delegate. delegate object has lifecycle independent of view. in many cases, delegate same object created/owns view in first place, example viewcontroller.
you not want strong reference goes "child" "parent". creates circular reference, , both child , parent hang around in memory until application exits.
in addition delegates , other "backwards-pointing" references, see weak references used in caches, want return object if it's requested second time, cache shouldn't keep object in memory if nobody's using it.
Comments
Post a Comment