swift - Does setting `constraint.isActive = false` deallocate the constraint? -
say want remove constraint, traditionally do:
view.removeconstraint(constraint)
however, there new isactive methods installing/uninstalling constraints.
if following:
constraint.isactive = false
will remove memory correctly?
yes,
constraint.isactive = false
is doing same thing as:
viewthatownsconstraint.removeconstraint(constraint)
so if thing holding on constraint view, correctly remove memory.
here's proof:
let view = uiview() weak var weakview: uiview? = nil autoreleasepool { weakview = uiview() } assert(weakview == nil) // traditional way of removing constraints ensures constraint deallocated weak var weakconstraint: nslayoutconstraint? = nil autoreleasepool { weakconstraint = view.widthanchor.constraint(equaltoconstant: 10) } assert(weakconstraint == nil) // nothing holding on constraint autoreleasepool { weakconstraint = view.widthanchor.constraint(equaltoconstant: 10) view.addconstraint(weakconstraint!) } assert(weakconstraint != nil) autoreleasepool { view.removeconstraint(weakconstraint!) } assert(weakconstraint == nil) // new way of removing constraints: assert(weakconstraint == nil) autoreleasepool { weakconstraint = view.widthanchor.constraint(equaltoconstant: 10) weakconstraint!.isactive = true } assert(weakconstraint != nil) autoreleasepool { weakconstraint!.isactive = false } assert(weakconstraint == nil)
Comments
Post a Comment