Skip to content

Commit

Permalink
Execute constraint copying asynchronously
Browse files Browse the repository at this point in the history
This fixes crashes where the replaced constraint is sill retained by a trait
collection but references a deallocated firstItem.

See sunnyxx#29
  • Loading branch information
dsmatter committed Sep 18, 2016
1 parent 57c6b77 commit 42b732e
Showing 1 changed file with 41 additions and 41 deletions.
82 changes: 41 additions & 41 deletions XXNibBridge/XXNibBridge.m
Original file line number Diff line number Diff line change
Expand Up @@ -70,50 +70,50 @@ + (UIView *)instantiateRealViewFromPlaceholder:(UIView *)placeholderView {

// Copy autolayout constrains.
if (placeholderView.constraints.count > 0) {

// We only need to copy "self" constraints (like width/height constraints)
// from placeholder to real view
for (NSLayoutConstraint *constraint in placeholderView.constraints) {

NSLayoutConstraint* newConstraint;

// "Height" or "Width" constraint
// "self" as its first item, no second item
if (!constraint.secondItem) {
newConstraint =
[NSLayoutConstraint constraintWithItem:realView
attribute:constraint.firstAttribute
relatedBy:constraint.relation
toItem:nil
attribute:constraint.secondAttribute
multiplier:constraint.multiplier
constant:constraint.constant];
}
// "Aspect ratio" constraint
// "self" as its first AND second item
else if ([constraint.firstItem isEqual:constraint.secondItem]) {
newConstraint =
[NSLayoutConstraint constraintWithItem:realView
attribute:constraint.firstAttribute
relatedBy:constraint.relation
toItem:realView
attribute:constraint.secondAttribute
multiplier:constraint.multiplier
constant:constraint.constant];
}

// Copy properties to new constraint
if (newConstraint) {
newConstraint.shouldBeArchived = constraint.shouldBeArchived;
newConstraint.priority = constraint.priority;
if ([UIDevice currentDevice].systemVersion.floatValue >= 7.0f) {
newConstraint.identifier = constraint.identifier;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// We only need to copy "self" constraints (like width/height constraints)
// from placeholder to real view
for (NSLayoutConstraint *constraint in placeholderView.constraints) {
NSLayoutConstraint* newConstraint;

// "Height" or "Width" constraint
// "self" as its first item, no second item
if (!constraint.secondItem) {
newConstraint =
[NSLayoutConstraint constraintWithItem:realView
attribute:constraint.firstAttribute
relatedBy:constraint.relation
toItem:nil
attribute:constraint.secondAttribute
multiplier:constraint.multiplier
constant:constraint.constant];
}
// "Aspect ratio" constraint
// "self" as its first AND second item
else if ([constraint.firstItem isEqual:constraint.secondItem]) {
newConstraint =
[NSLayoutConstraint constraintWithItem:realView
attribute:constraint.firstAttribute
relatedBy:constraint.relation
toItem:realView
attribute:constraint.secondAttribute
multiplier:constraint.multiplier
constant:constraint.constant];
}

// Copy properties to new constraint
if (newConstraint) {
newConstraint.shouldBeArchived = constraint.shouldBeArchived;
newConstraint.priority = constraint.priority;
if ([UIDevice currentDevice].systemVersion.floatValue >= 7.0f) {
newConstraint.identifier = constraint.identifier;
}
[realView addConstraint:newConstraint];
}
[realView addConstraint:newConstraint];
}
}
});
}

return realView;
}

Expand Down

0 comments on commit 42b732e

Please sign in to comment.