Skip to content

Commit

Permalink
Fix bug around view.snp.layoutConstraints being dealloc’d
Browse files Browse the repository at this point in the history
  • Loading branch information
robertjpayne committed Sep 7, 2016
1 parent ca34ad4 commit 2e4cebf
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
12 changes: 6 additions & 6 deletions Source/Constraint.swift
Expand Up @@ -46,7 +46,7 @@ public class Constraint {
self.updateConstantAndPriorityIfNeeded()
}
}
private let layoutConstraints: NSHashTable<LayoutConstraint>
private var layoutConstraints: [LayoutConstraint]

// MARK: Initialization

Expand All @@ -66,7 +66,7 @@ public class Constraint {
self.multiplier = multiplier
self.constant = constant
self.priority = priority
self.layoutConstraints = NSHashTable<LayoutConstraint>.weakObjects()
self.layoutConstraints = []

// get attributes
let layoutFromAttributes = self.from.attributes.layoutAttributes
Expand Down Expand Up @@ -157,7 +157,7 @@ public class Constraint {
layoutConstraint.constraint = self

// append
self.layoutConstraints.add(layoutConstraint)
self.layoutConstraints.append(layoutConstraint)
}
}

Expand Down Expand Up @@ -213,7 +213,7 @@ public class Constraint {
// MARK: Internal

internal func updateConstantAndPriorityIfNeeded() {
for layoutConstraint in self.layoutConstraints.allObjects {
for layoutConstraint in self.layoutConstraints {
let attribute = (layoutConstraint.secondAttribute == .notAnAttribute) ? layoutConstraint.firstAttribute : layoutConstraint.secondAttribute
layoutConstraint.constant = self.constant.constraintConstantTargetValueFor(layoutAttribute: attribute)
layoutConstraint.priority = self.priority.constraintPriorityTargetValue
Expand All @@ -222,7 +222,7 @@ public class Constraint {

internal func activateIfNeeded(updatingExisting: Bool = false) {
let view = self.from.view!
let layoutConstraints = self.layoutConstraints.allObjects
let layoutConstraints = self.layoutConstraints
let existingLayoutConstraints = view.snp.layoutConstraints

if updatingExisting && existingLayoutConstraints.count > 0 {
Expand All @@ -245,7 +245,7 @@ public class Constraint {

internal func deactivateIfNeeded() {
let view = self.from.view!
let layoutConstraints = self.layoutConstraints.allObjects
let layoutConstraints = self.layoutConstraints
NSLayoutConstraint.deactivate(layoutConstraints)
view.snp.remove(layoutConstraints: layoutConstraints)
}
Expand Down
29 changes: 24 additions & 5 deletions Source/ConstraintViewDSL.swift
Expand Up @@ -101,17 +101,36 @@ public struct ConstraintViewDSL: ConstraintAttributesDSL {
}

internal var layoutConstraints: [LayoutConstraint] {
return objc_getAssociatedObject(self.view, &layoutConstraintsKey) as? [LayoutConstraint] ?? []
return self.layoutConstraintsHashTable.allObjects
}

internal func add(layoutConstraints: [LayoutConstraint]) {
let merged = self.layoutConstraints + layoutConstraints
objc_setAssociatedObject(self.view, &layoutConstraintsKey, merged, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
let hashTable = self.layoutConstraintsHashTable
for layoutConstraint in layoutConstraints {
hashTable.add(layoutConstraint)
}
print(hashTable, self.layoutConstraintsHashTable)
}

internal func remove(layoutConstraints: [LayoutConstraint]) {
let merged = self.layoutConstraints.filter { !layoutConstraints.contains($0) }
objc_setAssociatedObject(self.view, &layoutConstraintsKey, merged, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
let hashTable = self.layoutConstraintsHashTable
for layoutConstraint in layoutConstraints {
hashTable.remove(layoutConstraint)
}
print(hashTable, self.layoutConstraintsHashTable)
}

private var layoutConstraintsHashTable: NSHashTable<LayoutConstraint> {
let layoutConstraints: NSHashTable<LayoutConstraint>

if let existing = objc_getAssociatedObject(self.view, &layoutConstraintsKey) as? NSHashTable<LayoutConstraint> {
layoutConstraints = existing
} else {
layoutConstraints = NSHashTable<LayoutConstraint>.weakObjects()
objc_setAssociatedObject(self.view, &layoutConstraintsKey, layoutConstraints, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
}
return layoutConstraints

}

}
Expand Down

0 comments on commit 2e4cebf

Please sign in to comment.