I have received the following crash report but I do not understand the issue.Line 487 points to a check if the pickerView in the delegate is a particular variable.
Crashed: com.apple.main-thread
0 App 0x1001c5208 specialized NewCorrectiveVC.pickerView(UIPickerView, didSelectRow : Int, inComponent : Int) -> () (NewCorrectiveVC.swift:487)
1 App 0x1001c2028 @objc NewCorrectiveVC.pickerView(UIPickerView, didSelectRow : Int, inComponent : Int) -> () (NewCorrectiveVC.swift)
2 UIKit 0x197a83154 -[UIPickerView _sendSelectionChangedForComponent:notify:] + 116
3 UIKit 0x197a8338c -[UIPickerView _sendSelectionChangedFromTable:notify:] + 344
4 UIKit 0x197fb0424 -[UIPickerTableView _scrollingFinished] + 188
5 UIKit 0x197fb05fc -[UIPickerTableView scrollViewDidEndDecelerating:] + 28
6 UIKit 0x197b216ac -[UIScrollView(UIScrollViewInternal) _scrollViewDidEndDeceleratingForDelegate] + 132
7 UIKit 0x1979b6db0 -[UIScrollView(UIScrollViewInternal) _stopScrollDecelerationNotify:] + 332
8 UIKit 0x1979b68ec -[UIScrollView _smoothScrollWithUpdateTime:] + 2356
9 QuartzCore 0x194bc01bc CA::Display::DisplayLinkItem::dispatch(unsigned long long) + 44
10 QuartzCore 0x194bc0068 CA::Display::DisplayLink::dispatch_items(unsigned long long, unsigned long long, unsigned long long) + 444
11 IOKit 0x191c27138 IODispatchCalloutFromCFMessage + 372
12 CoreFoundation 0x19195056c __CFMachPortPerform + 180
13 CoreFoundation 0x191968934 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 56
14 CoreFoundation 0x1919680e8 __CFRunLoopDoSource1 + 436
15 CoreFoundation 0x191965bcc __CFRunLoopRun + 1840
16 CoreFoundation 0x191894048 CFRunLoopRunSpecific + 444
17 GraphicsServices 0x19331a198 GSEventRunModal + 180
18 UIKit 0x1978792fc -[UIApplication _run] + 684
19 UIKit 0x197874034 UIApplicationMain + 208
20 App 0x100173d04 main (AppDelegate.swift:22)
21 libdispatch.dylib 0x1908785b8 (Missing)
Code:
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
{
if pickerView == assignedTo
{
if employees.count > 0 {
selectedEmp = employees[row].employeeId
}
}
else if pickerView == category \\this is line 487
{
if categories.count > 0 {
selectedCat = categories[row].wocategoryId
}
}
}
EDIT:
Not sure if the following is relevant but I do have observers linked to my pickerViews:
let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(self.longPressed))
assignedTo.addGestureRecognizer(longPressRecognizer)
let catLongPressRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(self.catLongPressed))
category.addGestureRecognizer(catLongPressRecognizer)
self.hideKeyboardWhenTappedAround()
self.scroll.keyboardDismissMode = .interactive
FYI:
public func hideKeyboardWhenTappedAround() {
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissKeyboard))
view.addGestureRecognizer(tap)
}
I suspect the problem is in the call to ==
.
if pickerView == assignedTo
This isn't really what you mean. This forces a call to the ==
function and tries to evaluate equality between these two objects. If one of them is nil
that will crash.
pickerView
should never be nil
, but I've encountered cases where UIKit sends nil
for parameters that should never be nil
. (This especially can happen if there are any calls to UIKit methods on background threads. Be very careful about not doing that, but it can also happen due to UIKit bugs.)
assignedTo
can be nil
if the view hasn't loaded. In theory you should never get this call in that case, but again, it's possible in the presence of bugs, again most commonly due to calling UIKit methods off the main thread (any UIKit methods; it doesn't always have to be related to this particular picker view), but UIKit sometimes has bugs of its own.
In any case, you don't mean "is pickerView equal to assignedTo." You mean "is pickerView exactly the same thing as assignedTo." That's ===
:
if pickerView === assignedTo
===
does a pointer comparison, so even in the face of an invalid nil
it's safe.
Keep in mind that this is almost certainly related to a race condition. The fact that it doesn't easily reproduce when you run it in the debugger doesn't mean anything. It might happen once in 10k tries, only in Release, exclusively on iPhone 6. That's the nature of race conditions.
来源:https://stackoverflow.com/questions/46170876/random-crash-on-pickerview-didselect-cfrunloop-is-calling-out-to-a-source1-p