iOS/Objective-C equivalent of Android's AsyncTask

前端 未结 5 1660
野的像风
野的像风 2020-12-07 09:07

I\'m familiar with using AsyncTask in Android: create a subclass, call execute on an instance of the subclass and onPostExecute is cal

5条回答
  •  自闭症患者
    2020-12-07 09:55

    Swift 3

    In Android when I wanted to run a task on a background thread and then update the UI when it finished, I used AsyncTask (example). Now when I am making iOS versions of my apps, I use Grand Central Dispatch (GCD) to do the same thing. Here is how it is done with Swift:

    DispatchQueue.global(qos: .background).async {
    
        // code to be run on a background task
    
        DispatchQueue.main.async {
    
            // code to be run on the main thread after the background task is finished
        }
    }
    

    Notes

    • Grand Central Dispatch Tutorial for Swift

提交回复
热议问题