简体   繁体   English

使用Task-c在iOS8 / 9中使用dataTaskWithRequest VS sendAsynchronousRequest

[英]dataTaskWithRequest VS sendAsynchronousRequest in iOS8/9 using Objective-c

Having an app written in Objective-C targeting iOS8/9 there are real vantange in performance or stability updating code using NSURLSession 使用以iOS 8/9为目标的Objective-C编写的应用程序,使用NSURLSession在性能或稳定性更新代码方面确实存在优势

NSURLSession *session = [NSURLSession sharedSession];
        [[session dataTaskWithRequest:request
                    completionHandler:^(NSData *data,
                                        NSURLResponse *response,
                                        NSError *error) {

                        dispatch_sync(dispatch_get_main_queue(), ^{
                              //UPDATE UI

                        });

                    }] resume];

in place of code like the following that use NSURLConnection: 代替使用NSURLConnection的以下代码:

   [NSURLConnection sendAsynchronousRequest:request
                                       queue:[CMRequestManager connectionQueue]
                           completionHandler:^(NSURLResponse *response,
                                               NSData *data, NSError *connectionError)
     {


         dispatch_sync(dispatch_get_main_queue(), ^{

             //UPDATE UI

         });

     }];

If you decide to run the code in a WatchKit extension or on tvOS at some point in the future, then yes. 如果您决定将来某个时候在WatchKit扩展程序或tvOS中运行代码,则可以。 Otherwise, if the snippet above is representative of the way you're using the API, then I probably wouldn't bother rewriting it. 否则,如果上面的代码段代表了您使用API​​的方式,那么我可能就不会再重写它了。 With that said, this is very much a matter of opinion. 话虽如此,这很大程度上是一个见解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM