iOS/Objective-C equivalent of Android's AsyncTask

前端 未结 5 1670
野的像风
野的像风 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:57

    if your targeting earlier iOS Version (than iOS 4 for Grand Central Dispatch) you could use the NSObject performSelector methods

    • Execute on Background Thread performSelectorInBackground:withObject:

    • And execute on MainThread performSelectorOnMainThread:withObject:waitUntilDone:

    This is an example:

    [self performSelectorInBackground:@selector(executeInBackground) withObject:nil];
    
    
    -(void) executeInBackground
    {
        NSLog(@"executeInBackground");
    
        [self performSelectorOnMainThread:@selector(executeOnMainThread) withObject:nil waitUntilDone:NO];
    }
    
    -(void) executeOnMainThread
    {
        NSLog(@"executeOnMainThread");
    }
    

提交回复
热议问题