Asynchronous method call using iOS 4

I want to call the method asynchronously. This is the method that receives HTML from the server and sets it to UIWebView :

 NSString *htmlTest = [BackendProxy getContent]; [webView loadHTMLString:htmlTest baseURL: nil]; [webView setUserInteractionEnabled:YES]; 

I want to start the activity indicator in UIWebView during data retrieval, so I need to call getContent asynchronously. How can i achieve this?

+4
source share
2 answers

I suggest performSelectorInBackground:withObject: of NSObject .

As below:

 - (void)loadIntoWebView: (id) dummy { NSString *html = [BackendProxy getContent]; [self performSelectorOnMainThread: @selector(loadingFinished:) withObject: html]; } - (void)loadingFinished: (NSString*) html { // stop activity indicator [webView loadHTMLString:html baseURL: nil]; [webView setUserInteractionEnabled:YES]; } - (void) foo { // ... // start activity indicator [self performSelectorInBackground: @selector(loadIntoWebView:) withObject: nil]; } 
+8
source

This is a great option for the GCD API, Apple new (ish) concurrency.

 dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); dispatch_async(queue, ^ { // Background work here NSLog(@"Finished work in background"); dispatch_async(dispatch_get_main_queue(), ^ { NSLog(@"Back on main thread"); }); }); 

Here's the documentation of the dispatch queue

+14
source

Source: https://habr.com/ru/post/1390877/


All Articles