在获取JSON时在iOS中使用spinKit显示微调器

时间:2016-05-11 07:06:24

标签: ios json dispatch-async

我要做的是,只需点击一下按钮,即可使用AFNetworking pod获取数据。我想要的是在获取数据之后我想要显示NSLog(@"/n /nAfter fetching");但它在json数据之前。

这是我在Button的IBAction中编写的代码。

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");

请提供一个很好的方法,并纠正我如果我错了。谢谢。

1 个答案:

答案 0 :(得分:1)

由于AFURLSessionManager进行异步操作。你将完成数据

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];

        });

如果你想使用URLSession进行同步操作,下面的方法可以帮助你

+ (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;  
}