SnapKit: Fatal error: updated constraint could not find existing matching constraint to update

New Issue Checklist

  • I have looked at the Documentation
  • I have read the F.A.Q.
  • I have filled out this issue template.

Issue Info

Info Value
Platform ios
Platform Version 12.2
SnapKit Version 5.0.0
Integration Method cocoapods

Issue Description

After SnapKit update (5.0.0 was 4.2.0) I have now fatal error on updateConstraints

Capture d’écran 2019-04-26 à 19 19 31

Updated constraint could not find existing matching constraint to update

After some investigations I discovered where the issue is located: LayoutConstraint.swift

The object comparison between firstAnchor or secondAnchor doesn’t work properly.

Capture d’écran 2019-04-26 à 18 53 52

I don’t understand the purpose of #574 because firstItem is unowned(unsafe) meaning it’ll crash if you try to update constraints for deallocated object

The real question is: Why are you trying to update constraints for deallocated objects 😄

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 4
  • Comments: 17 (6 by maintainers)

Most upvoted comments

@sashabelonogov, sure, it fails with margin properties, with topMargin for instance, but no crash with .top.

Simply use this sample code to test the crash:

import UIKit
import SnapKit

class ViewController: UIViewController {
  
  let snapView = UIView()

  override func viewDidLoad() {
    super.viewDidLoad()
    self.view.addSubview(self.snapView)
    self.snapView.snp.makeConstraints { (make) in
      make.topMargin.equalToSuperview()
    }
    
    self.snapView.snp.updateConstraints { (make) in
      make.topMargin.equalToSuperview()
    }
  }
}

encounter same problem

No. We just raised the regression about margins update crash, but no strong investigation started to fix it (simply rollback).

The point of the https://github.com/SnapKit/SnapKit/pull/574 is that the object that the constraints are updated for is not deallocated and the EXC_BAD_ACCESS is raised when the firstItem or secondItem are accessed. The problem is fixed with https://github.com/SnapKit/SnapKit/pull/574. Please, follow Apple documentation about this properties:

accessors
     firstItem.firstAttribute {==,<=,>=} secondItem.secondAttribute * multiplier + constant
     Access to these properties is not recommended. Use the `firstAnchor` and `secondAnchor` properties instead.