How to get API Data Asynchronously in Objective-C?

All my work is going fine, but There is a little problem in it. I have my NSURLRequest in -(void)viewDidLoad{} and it took some time to fetch data from server. I want it to be done in asynchronous way.

Following is my code please suggest me what should I implement.? Thanks in advance to all of you. :)

- (void)viewDidLoad {
[super viewDidLoad];
[[self tableView2]setDelegate:self ];
[[self tableView2]setDataSource:self];
array=[[NSMutableArray alloc]init];

NSString *castString = [NSString stringWithFormat:@"https://api.themoviedb.org/3/movie/%@/credits?api_key=c4bd81709e87b12e6c74a08609433c49",movieIDinString];
NSURL *url=[NSURL URLWithString:castString];

NSURLRequest *request=[NSURLRequest requestWithURL:url];
connection=[NSURLConnection connectionWithRequest:request delegate:self];
if (connection)
{
    webData=  [[NSMutableData alloc]init];
}

Upvotes: 0

Views: 904

Answers (3)

Bohm
Bohm

Reputation: 1004

Try AFNeworking. It provides several options for async downloads/uploads, with completions blocks.

Upvotes: 0

Sambit Prakash
Sambit Prakash

Reputation: 341

If you are using API, it will take some time, to fetch data from server. At this time, you have to use background thread and show activity indicator in main thread. After getting data from API, you need to change thread to main thread. Please check my below code.

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
    // background thread
    // code for API call

    dispatch_async(dispatch_get_main_queue(), ^{
        // main thread
    });
});

You can use callback method also.

[helperApi instaUserDetails:finderDetailsDataDict andCallback:^(id jsonResponse) {
        dispatch_async(dispatch_get_main_queue(), ^{
            if ([[jsonResponse objectForKey:@"code"] intValue] == 200) {

                userDetailsDict = [jsonResponse objectForKey:@"data"];

                mediaArray = [[[[jsonResponse objectForKey:@"data"] objectForKey:@"user"] objectForKey:@"media"] objectForKey:@"nodes"];
            }
            [activityIndicator stopAnimating];
            [self createUI];
        });
    }];

NSURLConnection is deprecated now. Try to use NSURLSession.

Upvotes: 1

Sudheer Kolasani
Sudheer Kolasani

Reputation: 283

try this..

  dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){
        //Background Thread

       NSString *castString = [NSString stringWithFormat:@"https://api.themoviedb.org/3/movie/%@/credits?api_key=c4bd81709e87b12e6c74a08609433c49",movieIDinString];
        NSURL *url=[NSURL URLWithString:castString];

        NSURLRequest *request=[NSURLRequest requestWithURL:url];
        connection=[NSURLConnection connectionWithRequest:request delegate:self];
        if (connection)
        {
            webData=  [[NSMutableData alloc]init];
        }

        dispatch_async(dispatch_get_main_queue(), ^(void){
            //Run UI Updates

// reload table view here
            [_activityIndicatorImageView stopAnimating];
        });
    });

Upvotes: 1

Related Questions