SnapKit / SnapKit

A Swift Autolayout DSL for iOS & OS X

Home Page:https://snapkit.github.io/SnapKit/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

SnapKit crashes when calling `updateConstraints`

zhouhao27 opened this issue · comments

New Issue Checklist

🚫 If this template is not filled out your issue will be closed with no comment. 🚫

  • 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 15.3.1
SnapKit Version 5.0.1
Integration Method cocoapods

Issue Description

The app crashes sometimes when calling updateConstraints.

0 libswiftCore.dylib 0x39b74 assertionFailure(:_:file:line🎏) + 308
1 SnapKit 0x8ec8 $s7SnapKit10ConstraintC16activateIfNeeded16updatingExistingySb_tF + 2820
2 SnapKit 0xe488 $s7SnapKit15ConstraintMakerC17updateConstraints4item7closureyAA06LayoutC4Item_p_yACXEtFZTf4enn_nSo13UILayoutGuideC_Tg5Tf4nnd_nTm + 260

My code:

    override func viewDidAppear() {
        super.viewDidAppear()
        self.view.translatesAutoresizingMaskIntoConstraints = false
        self.view.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(-100)
            make.leading.equalToSuperview().offset(22)
            make.trailing.equalToSuperview().offset(-26)
            make.height.equalTo(128)
        }
    }

    func doSomething() {
        self.view.snp.updateConstraints { make in
            make.height.equalTo(self.view.bounds.height + 50)
        }
    }

I suspected the function doSomething is called before the self.view.snp.makeConstraints. Then I move the self.view.snp.makeConstraints to viewDidLoad. But this will cause the other crash:

Expected superview but found nil when attempting make constraint equalToSuperview.

Any suggestion to prevent this kind of crash? Thanks.

override func viewDidAppear() {
super.viewDidAppear()
self.view.translatesAutoresizingMaskIntoConstraints = false
self.view.snp.makeConstraints { make in
make.top.equalToSuperview().offset(-100)
make.leading.equalToSuperview().offset(22)
make.trailing.equalToSuperview().offset(-26)
make.height.equalTo(128)
}
}

func doSomething() {
    self.view.snp.updateConstraints { make in
        make.height.equalTo(self.view.bounds.height + 50)
    }
}

Having the same issue here as well. Did you find any solution?

We were able to reproduce it by triggering this method couple of times in a short period of time. Once we removed that, we were no longer able to reproduce it. However, now we can see in our Crashlytics that the crash still exists however we no longer can reproduce it.

Having the same issue here as well. Did you find any solution?

We were able to reproduce it by triggering this method couple of times in a short period of time. Once we removed that, we were no longer able to reproduce it. However, now we can see in our Crashlytics that the crash still exists however we no longer can reproduce it.

New Issue Checklist

🚫 If this template is not filled out your issue will be closed with no comment. 🚫

  • 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 | 15.3.1

SnapKit Version | 5.0.1

Integration Method | cocoapods

Issue Description

The app crashes sometimes when calling updateConstraints.

0 libswiftCore.dylib 0x39b74 assertionFailure(:_:file:line🎏) + 308

1 SnapKit 0x8ec8 $s7SnapKit10ConstraintC16activateIfNeeded16updatingExistingySb_tF + 2820

2 SnapKit 0xe488 $s7SnapKit15ConstraintMakerC17updateConstraints4item7closureyAA06LayoutC4Item_p_yACXEtFZTf4enn_nSo13UILayoutGuideC_Tg5Tf4nnd_nTm + 260

My code:

    override func viewDidAppear() {

        super.viewDidAppear()

        self.view.translatesAutoresizingMaskIntoConstraints = false

        self.view.snp.makeConstraints { make in

            make.top.equalToSuperview().offset(-100)

            make.leading.equalToSuperview().offset(22)

            make.trailing.equalToSuperview().offset(-26)

            make.height.equalTo(128)

        }

    }



    func doSomething() {

        self.view.snp.updateConstraints { make in

            make.height.equalTo(self.view.bounds.height + 50)

        }

    }

I suspected the function doSomething is called before the self.view.snp.makeConstraints. Then I move the self.view.snp.makeConstraints to viewDidLoad. But this will cause the other crash:

Expected superview but found nil when attempting make constraint equalToSuperview.

Any suggestion to prevent this kind of crash? Thanks.

https://github.com/SnapKit/SnapKit/issues/752#issue-1209122751

override func viewDidAppear () { super.viewDidAppear () self.view.translatesAutoresizingMaskIntoConstraints = false self.view.snp.makeConstraints {make in make.top.equalToSuperview (). offset (-100) make.leading.equalToSuperview (). offset (22) make.trailing.equalToSuperview (). offset (-26) make.height.equalTo (128) } }

func doSomething() {
    self.view.snp.updateConstraints { make in
        make.height.equalTo(self.view.bounds.height + 50)
    }
}

#752 (comment)

  1. The question is, why do you need self.view.bounds.height, are the height view changing that doesn't specifically?
  2. Why don't you change the height value instead of updating the new constraint by updateConstraint?

Same issue, any update on this issue?

You shouldn't be using makeConstraints in "runs more than once" situations. viewDidAppear may be called more than once (I know for a fact if you push and pop in a navigation controller it will).

Instead use remakeConstraints or move all your constraint code to viewDidLoad.

If that doesn't resolve the issue try and isolate an example that can reproduce it if possible!