Fix up memory leaks

This commit is contained in:
Robert Payne 2016-09-20 16:53:32 +12:00
parent 505ed036a0
commit cfa4c6a545
4 changed files with 32 additions and 26 deletions

View File

@ -231,9 +231,12 @@ public class Constraint {
} }
internal func activateIfNeeded(updatingExisting: Bool = false) { internal func activateIfNeeded(updatingExisting: Bool = false) {
let view = self.from.view! guard let view = self.from.view else {
print("WARNING: SnapKit failed to get from view from constraint. Activate will be a no-op.")
return
}
let layoutConstraints = self.layoutConstraints let layoutConstraints = self.layoutConstraints
let existingLayoutConstraints = view.snp.layoutConstraints let existingLayoutConstraints = view.snp.constraints.map({ $0.layoutConstraints }).reduce([]) { $0 + $1 }
if updatingExisting { if updatingExisting {
for layoutConstraint in layoutConstraints { for layoutConstraint in layoutConstraints {
@ -247,14 +250,17 @@ public class Constraint {
} }
} else { } else {
NSLayoutConstraint.activate(layoutConstraints) NSLayoutConstraint.activate(layoutConstraints)
view.snp.add(layoutConstraints: layoutConstraints) view.snp.add(constraints: [self])
} }
} }
internal func deactivateIfNeeded() { internal func deactivateIfNeeded() {
let view = self.from.view! guard let view = self.from.view else {
print("WARNING: SnapKit failed to get from view from constraint. Deactivate will be a no-op.")
return
}
let layoutConstraints = self.layoutConstraints let layoutConstraints = self.layoutConstraints
NSLayoutConstraint.deactivate(layoutConstraints) NSLayoutConstraint.deactivate(layoutConstraints)
view.snp.remove(layoutConstraints: layoutConstraints) view.snp.remove(constraints: [self])
} }
} }

View File

@ -180,7 +180,7 @@ public class ConstraintMaker {
} }
internal static func updateConstraints(view: ConstraintView, closure: (_ make: ConstraintMaker) -> Void) { internal static func updateConstraints(view: ConstraintView, closure: (_ make: ConstraintMaker) -> Void) {
guard view.snp.layoutConstraints.count > 0 else { guard view.snp.constraints.count > 0 else {
self.makeConstraints(view: view, closure: closure) self.makeConstraints(view: view, closure: closure)
return return
} }
@ -197,7 +197,7 @@ public class ConstraintMaker {
} }
internal static func removeConstraints(view: ConstraintView) { internal static func removeConstraints(view: ConstraintView) {
let constraints = view.snp.layoutConstraints.map { $0.constraint! } let constraints = view.snp.constraints
for constraint in constraints { for constraint in constraints {
constraint.deactivateIfNeeded() constraint.deactivateIfNeeded()
} }

View File

@ -100,36 +100,36 @@ public struct ConstraintViewDSL: ConstraintAttributesDSL {
} }
internal var layoutConstraints: [LayoutConstraint] { internal var constraints: [Constraint] {
return self.layoutConstraintsHashTable.allObjects return self.constraintsHashTable.allObjects
} }
internal func add(layoutConstraints: [LayoutConstraint]) { internal func add(constraints: [Constraint]) {
let hashTable = self.layoutConstraintsHashTable let hashTable = self.constraintsHashTable
for layoutConstraint in layoutConstraints { for constraint in constraints {
hashTable.add(layoutConstraint) hashTable.add(constraint)
} }
} }
internal func remove(layoutConstraints: [LayoutConstraint]) { internal func remove(constraints: [Constraint]) {
let hashTable = self.layoutConstraintsHashTable let hashTable = self.constraintsHashTable
for layoutConstraint in layoutConstraints { for constraint in constraints {
hashTable.remove(layoutConstraint) hashTable.remove(constraint)
} }
} }
private var layoutConstraintsHashTable: NSHashTable<LayoutConstraint> { private var constraintsHashTable: NSHashTable<Constraint> {
let layoutConstraints: NSHashTable<LayoutConstraint> let constraints: NSHashTable<Constraint>
if let existing = objc_getAssociatedObject(self.view, &layoutConstraintsKey) as? NSHashTable<LayoutConstraint> { if let existing = objc_getAssociatedObject(self.view, &constraintsKey) as? NSHashTable<Constraint> {
layoutConstraints = existing constraints = existing
} else { } else {
layoutConstraints = NSHashTable<LayoutConstraint>.weakObjects() constraints = NSHashTable<Constraint>()
objc_setAssociatedObject(self.view, &layoutConstraintsKey, layoutConstraints, .OBJC_ASSOCIATION_RETAIN_NONATOMIC) objc_setAssociatedObject(self.view, &constraintsKey, constraints, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
} }
return layoutConstraints return constraints
} }
} }
private var layoutConstraintsKey: UInt8 = 0 private var constraintsKey: UInt8 = 0

View File

@ -39,7 +39,7 @@ public class LayoutConstraint: NSLayoutConstraint {
} }
} }
internal var constraint: Constraint! = nil internal weak var constraint: Constraint? = nil
} }