mirror of https://github.com/SnapKit/SnapKit
Fix up memory leaks
This commit is contained in:
parent
505ed036a0
commit
cfa4c6a545
|
@ -231,9 +231,12 @@ public class Constraint {
|
|||
}
|
||||
|
||||
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 existingLayoutConstraints = view.snp.layoutConstraints
|
||||
let existingLayoutConstraints = view.snp.constraints.map({ $0.layoutConstraints }).reduce([]) { $0 + $1 }
|
||||
|
||||
if updatingExisting {
|
||||
for layoutConstraint in layoutConstraints {
|
||||
|
@ -247,14 +250,17 @@ public class Constraint {
|
|||
}
|
||||
} else {
|
||||
NSLayoutConstraint.activate(layoutConstraints)
|
||||
view.snp.add(layoutConstraints: layoutConstraints)
|
||||
view.snp.add(constraints: [self])
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
NSLayoutConstraint.deactivate(layoutConstraints)
|
||||
view.snp.remove(layoutConstraints: layoutConstraints)
|
||||
view.snp.remove(constraints: [self])
|
||||
}
|
||||
}
|
||||
|
|
|
@ -180,7 +180,7 @@ public class ConstraintMaker {
|
|||
}
|
||||
|
||||
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)
|
||||
return
|
||||
}
|
||||
|
@ -197,7 +197,7 @@ public class ConstraintMaker {
|
|||
}
|
||||
|
||||
internal static func removeConstraints(view: ConstraintView) {
|
||||
let constraints = view.snp.layoutConstraints.map { $0.constraint! }
|
||||
let constraints = view.snp.constraints
|
||||
for constraint in constraints {
|
||||
constraint.deactivateIfNeeded()
|
||||
}
|
||||
|
|
|
@ -100,36 +100,36 @@ public struct ConstraintViewDSL: ConstraintAttributesDSL {
|
|||
|
||||
}
|
||||
|
||||
internal var layoutConstraints: [LayoutConstraint] {
|
||||
return self.layoutConstraintsHashTable.allObjects
|
||||
internal var constraints: [Constraint] {
|
||||
return self.constraintsHashTable.allObjects
|
||||
}
|
||||
|
||||
internal func add(layoutConstraints: [LayoutConstraint]) {
|
||||
let hashTable = self.layoutConstraintsHashTable
|
||||
for layoutConstraint in layoutConstraints {
|
||||
hashTable.add(layoutConstraint)
|
||||
internal func add(constraints: [Constraint]) {
|
||||
let hashTable = self.constraintsHashTable
|
||||
for constraint in constraints {
|
||||
hashTable.add(constraint)
|
||||
}
|
||||
}
|
||||
|
||||
internal func remove(layoutConstraints: [LayoutConstraint]) {
|
||||
let hashTable = self.layoutConstraintsHashTable
|
||||
for layoutConstraint in layoutConstraints {
|
||||
hashTable.remove(layoutConstraint)
|
||||
internal func remove(constraints: [Constraint]) {
|
||||
let hashTable = self.constraintsHashTable
|
||||
for constraint in constraints {
|
||||
hashTable.remove(constraint)
|
||||
}
|
||||
}
|
||||
|
||||
private var layoutConstraintsHashTable: NSHashTable<LayoutConstraint> {
|
||||
let layoutConstraints: NSHashTable<LayoutConstraint>
|
||||
private var constraintsHashTable: NSHashTable<Constraint> {
|
||||
let constraints: NSHashTable<Constraint>
|
||||
|
||||
if let existing = objc_getAssociatedObject(self.view, &layoutConstraintsKey) as? NSHashTable<LayoutConstraint> {
|
||||
layoutConstraints = existing
|
||||
if let existing = objc_getAssociatedObject(self.view, &constraintsKey) as? NSHashTable<Constraint> {
|
||||
constraints = existing
|
||||
} else {
|
||||
layoutConstraints = NSHashTable<LayoutConstraint>.weakObjects()
|
||||
objc_setAssociatedObject(self.view, &layoutConstraintsKey, layoutConstraints, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
|
||||
constraints = NSHashTable<Constraint>()
|
||||
objc_setAssociatedObject(self.view, &constraintsKey, constraints, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
|
||||
}
|
||||
return layoutConstraints
|
||||
return constraints
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
private var layoutConstraintsKey: UInt8 = 0
|
||||
private var constraintsKey: UInt8 = 0
|
||||
|
|
|
@ -39,7 +39,7 @@ public class LayoutConstraint: NSLayoutConstraint {
|
|||
}
|
||||
}
|
||||
|
||||
internal var constraint: Constraint! = nil
|
||||
internal weak var constraint: Constraint? = nil
|
||||
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue