I have a UIPickerView in my controller class and it is show when user touches a button.
private var periodPicker = UIPickerView()
I have two components in it for years and months.
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 2
}
Now I though that the numberOfRowsInComponent
function is ran only two times in the beginning - one for one component and second for the other - and then it could be ran whenever a component is reloaded. I don't reload any of my two components, however when I insert a print in numberOfRowsInComponent
function I get this output:
function:
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if component == 0 {
return years.count
} else {
print("numberOfRowsInComponent")
loadMonths()
return months.count
}
}
output:
numberOfRowsInComponent
numberOfRowsInComponent
numberOfRowsInComponent
<...>
numberOfRowsInComponent
numberOfRowsInComponent
My function is ran 19times! Is that normal behaviour from UIPickerView or not? Because it might impact my app's performance if it's not, right?
Unfortunately, you'd need an Apple employee who works on UIKit to tell you why. What I can tell you is that this appears to be normal. I threw together a small sample project that does nothing interesting to investigate this and it seems a large number of calls to this function also happen in a small view controller with a picker view and nothing else.
I would say whether or not this impacts your app's performance depends on what you do with the fact that this is called a lot of times. It doesn't really matter if it's normal behavior because you and I cannot fix it. But, if you don't want it to impact performance, don't do heavy things in that function.
If loadMonths
is heavy, maybe it's better to do elsewhere if you start having performance problems.