fermoya / SwiftUIPager

Native Pager in SwiftUI
MIT License
1.29k stars 172 forks source link

[BUG] Infinite loop if page is a @Published variable from an ObservableObject #164

Closed metsfan closed 3 years ago

metsfan commented 3 years ago

Describe the bug This seems to be a new bug after updating from 1.11.0 to 1.14.0. If the $page binding is set to a @Published property of an ObservableObject, there will be an infinite loop. Pager.swift on line 198, the page is being set onReceive, which triggers a change to the Published property, which triggers a view refresh, and this cycle will continue infinitely. This could be fixed by simply checking to be sure that the value is different before updating the page.

I was able to work around this by putting a local @State variable in my view which I then listen for and only make the state update to my ViewModel if the value is actually different but IMO this should be handled by the pager to reduce extra potential page renders.

To Reproduce Bind the page variable to a @Published property from an ObservableObject

Expected behavior The pager will loop infinitely and the app will never start

Environment:

fermoya commented 3 years ago

Hi @metsfan , thanks for reporting. I’ve prepared a pull-request for it, is this the change you suggest?

I’ve released a beta version and once I run some tests tomorrow I’ll have a hotfix release ready

metsfan commented 3 years ago

Yes, thanks you!

fermoya commented 3 years ago

Version 1.14.1 is live now