简体   繁体   中英

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

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 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. Otherwise, if the snippet above is representative of the way you're using the API, then I probably wouldn't bother rewriting it. With that said, this is very much a matter of opinion.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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