什么是diff b / w同步和异步请求,如何以编程方式检查此操作

时间:2015-12-12 06:49:12

标签: ios objective-c nsurlconnection

您好我是ios的初学者,当我们使用NSURLRequest呼叫服务时,我想知道想要知道当我们使用"同步请求"呼叫服务时发生了什么。以编程方式使用异步请求调用服务, 请以编程方式解释该操作,并使用该代码解释同步和异步操作

在下面编写了一些代码

我的代码: -

- (void)viewDidLoad {
    [super viewDidLoad];

 NSURL *url = [NSURL URLWithString:@"http://api.kivaws.org/v1/loans/search.json?status=fundraising"];
        NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];

        [theRequest setHTTPMethod:@"GET"];
        [theRequest setValue:@"application/json" forHTTPHeaderField:@"Accept"];
        [theRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
        [theRequest setTimeoutInterval:5];

        NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

        if(connection){

            webData = [[NSMutableData alloc] init];
        }
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {

    [webData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {

    [webData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {

    NSLog(@"error is %@",[error localizedDescription]);
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {

    NSString * allDataDictionbary = [[NSString alloc] initWithData:webData encoding:NSUTF8StringEncoding];

    NSArray * responseString = [allDataDictionbary JSONValue];

   NSLog(@"final respone dictionary%@",responseString);
   }

4 个答案:

答案 0 :(得分:4)

在回答您的问题时,同步请求会阻止调用它们的线程,直到请求完成。 (并且由于这个原因,通常不鼓励同步请求。)异步请求允许当前线程在执行请求时继续执行(例如,继续响应用户与应用程序的交互;响应系统事件;等)。这通常是优选的。

您的代码正在执行异步请求(这很好)。但是有一些问题:

  1. 您不应再使用NSURLConnection了。它被弃用了。使用NSURLSession。它实际上更简单,因为你通常不必编写那些委托方法(除非你想要,因为你有一些迫切的需要这样做)。

    有关NSURLSession的详细信息,请参阅 URL会话编程指南中的Using NSURLSession或参阅WWDC 2013视频What's New in Foundation Networking以获得详细介绍。< / p>

  2. 您没有进行一些错误处理。你正在检查基本错误(例如没有网络),这是非常好的,但你没有考虑其他可能不会导致NSError对象的Web服务器错误,但可能只是导致HTTP状态代码不是200.我建议检查一下。

    有关HTTP状态代码的列表,请参阅RFC 2616的第10部分。

  3. 您正在设置Content-Type application/json。但这不是JSON请求。 (当然,响应是JSON,但请求不是。)通常你会使用application/x-www-form-urlencoded来处理这样的请求。

  4. 在您的代码段中,您建议服务器的响应是JSON,其中NSArray作为顶级对象。但顶级对象是NSDictionary

  5. 您正在使用JSONValue。我不确定是哪个JSON库,但我们很多人只使用Apple提供的内置NSJSONSerialization类。很久以前,在Apple提供NSJSONSerialization之前,我们会使用第三方库来解析JSON,但不再需要。

  6. 使用NSURLSession发送请求的正确方法如下:

    NSURL *url = [NSURL URLWithString:@"http://api.kivaws.org/v1/loans/search.json?status=fundraising"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    
    [request setHTTPMethod:@"GET"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    
    NSURLSessionTask *task = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        if (error != nil) {
            NSLog(@"fundamental network error = %@", error);
            return;
        }
    
        if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
            NSInteger statusCode = [(NSHTTPURLResponse *)response statusCode];
            if (statusCode != 200) {
                NSLog(@"Warning; server should respond with 200 status code, but returned %ld", (long)statusCode);
            }
        }
    
        NSError *parseError;
        NSDictionary *responseObject = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];
        if (responseObject) {
            NSLog(@"responseObject = %@", responseObject);
        } else {
            NSLog(@"Error parsing JSON: %@", parseError);
            NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
            NSLog(@"responseString = %@", responseString);
        }
    }];
    [task resume];
    
    // Note, you'll reach this portion of your code before the
    // above `completionHandler` runs because this request runs
    // asynchronously. So put code that uses the network response
    // above, inside that `completionHandler`, not here.
    

答案 1 :(得分:0)

同步请求阻塞线程直到完成。当异步请求创建单独的线程并执行时,在完成时返回主线程。

当您运行此行时,时间差异为1秒或取决于净速度

 NSLog(@"before SynchronousRequest time %@",[NSDate date]);
    NSError *error = nil;
    NSHTTPURLResponse *response = nil;
    NSData *data=[NSURLConnection sendSynchronousRequest:request returningResponse:&response   error:&error];
    NSLog(@"after SynchronousRequest time %@",[NSDate date]);

在SynchronousRequest时间2015-12-12 09:26:01 +0000

之前

在SynchronousRequest时间2015-12-12 09:26:02 +0000

之后

当你运行这条线时没有时间差异

 NSLog(@"before AsynchronousRequest time %@",[NSDate date]);
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
        if (data) {
            NSString *str = [[NSString alloc] initWithData:data
                                                 encoding:NSUTF8StringEncoding];  // note the retain count here.
            NSLog(@"%@",str);
        } else {
            // handle error
        }
    }];
    NSLog(@"after AsynchronousRequest time %@",[NSDate date]);

在AsynchronousRequest时间之前2015-12-12 09:29:50 +0000

AsynchronousRequest时间2015-12-12 09:29:50 +0000

答案 2 :(得分:0)

当从一个线程发起HTTP请求时,如果你不介意耐心(#34;感谢你等待&#34),线程可以决定自己处理你的回复。 )。该线程被阻塞,直到HTTP请求完全处理完毕。或者,线程可以认为他有更好的事情要做,而不是等待冗长的HTTP事务,因此他很高兴地将任务传递给另一个进程来处理HTTP事务,以及回调以便他们保持联系。当另一个进程完成时,它会使用回调给它的发起者完成它。

答案 3 :(得分:0)

把它想象成一条道路。想象一下,如果没有告知其应用,您的应用程序将只在一条路上运行。现在,您应用中发生的一切都必须保持这条路。如果任何&#34;汽车&#34;(阅读:任务)花费的时间太长,整个事情就会关闭,而你的道路&#34;(阅读:应用程序)会变得没有响应。在异步操作中,您创建另一条道路(读取:线程),慢速行驶的汽车可以切换到该道路。一旦它的速度足以在主要道路上运行,它就可以切换回来。

相关问题