Sharad Chauhan
Sharad Chauhan

Reputation: 4891

Showing spinner using spinKit in iOS while fetching JSON

What I am trying to do is, on a click of a button I am fetching data using AFNetworking pod. What i want is after fetching data I want to display NSLog(@"/n /nAfter fetching"); but it is coming before the json data.

Here is the code I have written in IBAction of Button.

dispatch_queue_t queue = dispatch_get_global_queue(
                                                           DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
        dispatch_sync(queue, ^{
            //Load the json on another thread

            NSURL *url = [NSURL URLWithString:finalURL];

            NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
            AFURLSessionManager *manager = [[AFURLSessionManager alloc]initWithSessionConfiguration:configuration];

            NSURLRequest *request = [NSURLRequest requestWithURL:url];

            NSURLSessionDataTask *dataTask = [manager dataTaskWithRequest:request completionHandler:^(NSURLResponse *response,id responseObject, NSError *error) {

                if (error) {
                    NSLog(@"\n Error --> %@",error);
                }
                else{
                    jsonDictionary = (NSDictionary*) responseObject;
                    NSLog(@"/n Data :: %@",jsonDictionary);
                }
            }];
            [dataTask resume];

        });

        NSLog(@"/n /nAfter fetching");

Please provide a nice way of doing it and do correct me If I have gone wrong with it. Thank you.

Upvotes: 1

Views: 83

Answers (1)

Rajesh
Rajesh

Reputation: 10434

As AFURLSessionManager makes the Asynchronous operation. YOu will get the data in completion

dispatch_queue_t queue = dispatch_get_global_queue(
                                                           DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
        dispatch_sync(queue, ^{
            //Load the json on another thread
[self startSpinner];
            NSURL *url = [NSURL URLWithString:finalURL];

            NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
            AFURLSessionManager *manager = [[AFURLSessionManager alloc]initWithSessionConfiguration:configuration];

            NSURLRequest *request = [NSURLRequest requestWithURL:url];

            NSURLSessionDataTask *dataTask = [manager dataTaskWithRequest:request completionHandler:^(NSURLResponse *response,id responseObject, NSError *error) {
    [self stopSpinner];

                if (error) {
                    NSLog(@"\n Error --> %@",error);
                }
                else{

        NSLog(@"/n /nAfter fetching");  //this is where you receive data
                    jsonDictionary = (NSDictionary*) responseObject;
                    NSLog(@"/n Data :: %@",jsonDictionary);
                }
            }];
            [dataTask resume];

        });

if you want to make synchronous operation using URLSession below method will help you

+ (NSData *)sendSynchronousRequest:(NSURLRequest *)request  
    returningResponse:(__autoreleasing NSURLResponse **)responsePtr  
    error:(__autoreleasing NSError **)errorPtr {  
    dispatch_semaphore_t    sem;  
    __block NSData *        result;  

    result = nil;  

    sem = dispatch_semaphore_create(0);  

    [[[NSURLSession sharedSession] dataTaskWithRequest:request  
        completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {  
        if (errorPtr != NULL) {  
            *errorPtr = error;  
        }  
        if (responsePtr != NULL) {  
            *responsePtr = response;  
        }  
        if (error == nil) {  
            result = data;  
        }  
        dispatch_semaphore_signal(sem);  
    }] resume];  

    dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);  

   return result;  
} 

Upvotes: 1

Related Questions