-2

I'm trying to create UIPickerView programmatically and for this I did:

var pickerView = UIPickerView()

override func viewDidLoad() {
    super.viewDidLoad()

    self.pickerView.delegate = self
    self.pickerView.dataSource = self
}

but I get really annoying errors according to threads as:

Main Thread Checker: UI API called on a background thread: -[UIPickerView init]
PID: 70649, TID: 6408323, Thread name: (none), 
Queue name: NSOperationQueue 0x60000023ba20 (QOS: UNSPECIFIED), QoS: 0

It's really strange because it worked before and it started to appear exactly now!

I tried to change it as:

var pickerView: UIPickerView!

override func viewDidLoad() {
    super.viewDidLoad()

    Dispatch.main.async {
      self.pickerView = UIPickerView()
      self.pickerView.delegate = self
      self.pickerView.dataSource = self
    }
}

but I got crash again. Another error log was:

Assertion failure in void _UIPerformResizeOfTextViewForTextContainer(NSLayoutManager *, UIView<NSTextContainerView> *, NSTextContainer *, NSUInteger)(), /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIFoundation_Sim/UIFoundation-546.4/UIFoundation/TextSystem/NSLayoutManager_Private.m:1619

I'm spending my second hour in it and cannot realize what is the problem here. Can someone help me to understand?

J. Doe
  • 521
  • 4
  • 15
  • Where did you load/init that ViewController? I guess that the issue? – Larme May 21 '18 at 18:25
  • @matt I'm going to that screen with `prepareForSegue` and I do nothing with `UIPickerView` there. I just want to show that picker as `inputView`, that's it. I can show other parts of my code also, you just ask – J. Doe May 21 '18 at 18:28
  • @Larme I'm going to that screen with prepareForSegue. VC does not have any `init` method in it – J. Doe May 21 '18 at 18:29
  • where do you run performSegue ?? – Shehata Gamal May 21 '18 at 18:29
  • Where do tou call prepareForSegue() then? The init method is hidden through the segue perform. – Larme May 21 '18 at 18:30
  • @Larme I did not get your question. What that means `where?`??? In my previous controller? – J. Doe May 21 '18 at 18:31
  • Show that code and call the performsegue in main thread, because it seems that you are not. But since it's UI, all UI stuff needs to be done in main thread. – Larme May 21 '18 at 18:32
  • @Larme you mean the code inside of `prepareForSegue` like `if segue.identifier == "newVC" {`? – J. Doe May 21 '18 at 18:34
  • 1
    Not the prepare, the `performSegue(withIndentifier:)` or something like that. – Larme May 21 '18 at 18:34
  • @Larme thank you! It really helped me! I did not think about that part at all! – J. Doe May 21 '18 at 18:36
  • That's the same issue (first one which is the crash) as there: https://stackoverflow.com/questions/47207671/attempting-to-segue-from-objective-c-to-swift-vc/47220787#47220787 It's in Objective-C, but since we use CocoaTouch, language doesn't matter. – Larme May 21 '18 at 18:38

1 Answers1

1

The problem lies, not in any code you have shown (inside the view controller), but in the code that summons the view controller. How does the view controller come into existence? For example, if it's because you call performSegue, you need to make sure you're calling performSegue on the main thread.

matt
  • 515,959
  • 87
  • 875
  • 1,141