Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

SnapKit crashes when calling updateConstraints #752

Open
3 tasks done
zhouhao27 opened this issue Apr 20, 2022 · 8 comments
Open
3 tasks done

SnapKit crashes when calling updateConstraints #752

zhouhao27 opened this issue Apr 20, 2022 · 8 comments

Comments

@zhouhao27
Copy link

zhouhao27 commented Apr 20, 2022

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.

@Deloww
Copy link

Deloww commented May 4, 2022

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)
    }
}

@shvetsjr
Copy link

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.

@Deloww
Copy link

Deloww commented Jul 5, 2022

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.

@Deloww
Copy link

Deloww commented Jul 5, 2022

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.

@Deloww
Copy link

Deloww commented Jul 5, 2022

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)

@lutluthfi
Copy link

lutluthfi commented Sep 13, 2022

  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?

@zyflovelam
Copy link

Same issue, any update on this issue?

@robertjpayne
Copy link
Member

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants