5

Ever since I am working with preferences (PreferenceKey,..), I receive this message in the console:

Bound preference _ tried to update multiple times per frame.

After countless times of research, I haven't found any way to silence it. So... since there is yet no question specifically to this warning, what would you think are possible reasons?

If not, can this warning be ignored or did I have to fix it?

Thank you so much!

(I have tried to find an example, but somehow I didn't get any warnings with an easy one...)

zeytin
  • 4,220
  • 4
  • 10
  • 36
Mofawaw
  • 2,576
  • 1
  • 12
  • 37
  • 1
    I'm experiencing the same issue. In my case, I'm using anchorPreferences inside a List. https://stackoverflow.com/questions/65386898/swiftui-anchorpreference-inside-list – Aнгел Dec 21 '20 at 02:36

2 Answers2

2

The SwiftUI change handlers such as onPreferenceChange are called on an arbitrary thread. So if these changes impact your View, you should re-dispatch to ensure that you make those updates on the main thread:

.onPreferenceChange(MyPreferenceKey.self) { newValue in
    DispatchQueue.main.async {
        widget.value = newValue
    }
}
mbxDev
  • 565
  • 4
  • 16
  • 2
    Hmm. This didn't solve it for me. In my case, I'm using the same PreferenceKey in numerous different Views. Is that a valid approach? Specifically, I'm determining the widths of certain Views for purpose of sizing sister Views. – Matthew Rips Mar 20 '21 at 18:16
  • not solved for me either. – Duck May 17 '21 at 16:04
0

I am building a calendar app, based on the month it set the choseDate as initial value, when not using main queue, it goes into an infinite loop with messages like onChange(of: String) action tried to update multiple times per frame. After change to use main queue solved my problem.

                DispatchQueue.main.async {
                    dateChosen = Date()
                }
Harry
  • 1
  • 1