Error message that UIKit should not be called from a secondary thread

前端 未结 2 829
囚心锁ツ
囚心锁ツ 2020-12-17 07:21

I have an app which uses a UISearchBar to dynamically search from an external API based on user input.

The app is searching the external API fine and d

相关标签:
2条回答
  • 2020-12-17 07:46

    "Tried to obtain the web lock from a thread other than the main thread or the web thread UIKit should not be called from a secondary thread"

    The fix is conceptually simple; don't update the UI from your thread.

    Assuming the parseDidComplete is where the message is sourced, then something like this will "work":

    [delegate performSelectorOnMainThread: @selector(parseDidComplete) withObject: nil waitUntilDone: YES];
    

    "Work" because threading is hard and this answer completely ignores any synchronization issues you might have.

    Note that you'd be better off using NSOperation and NSOperationQueue. They are well documented and there are a bunch of examples.

    0 讨论(0)
  • 2020-12-17 07:56

    I would suspect the line:

    [delegate parseDidComplete];
    

    If the delegate class is interacting with UIKit components, then the background thread that is retrieved the XML contents is then calling the front-end objects which must all be in the main thread.

    You may want to look at using an NSOperation and NSOperationQueue to do the asynchronous operations. I believe that provides a more threadsafe way to handle this type of use case.

    0 讨论(0)
提交回复
热议问题