1

I have an iOS method that is now deprecated --NSURLConnection sendSynchronousRequest. This method worked and was fast.

I must be doing something wrong with the new method, as it is unacceptably slow.

The new method code I'm showing the whole routine is:

- (void)getData {
    NSLog(@"%s", __FUNCTION__);

    pathString = @"https://api.wm.com/json/jRoutes/.......";

    NSURL *url = [NSURL URLWithString:pathString......];

    NSURLSessionDataTask *downloadTask = [[NSURLSession sharedSession]
                                          dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

                                              if ([response respondsToSelector:@selector(statusCode)]) {
                                                  if ([(NSHTTPURLResponse *) response statusCode] == 404) {
                                                      dispatch_async(dispatch_get_main_queue(), ^{
                                                          // alert
                                                          NSLog(@" NO DATA");
                                                          return;
                                                      });
                                                  }
                                              }

                                              // 4: Handle response here
                                              [self processResponseUsingData:data];
                                          }];

     [downloadTask resume];
}


- (void)processResponseUsingData:(NSData*)data {
    NSLog(@"%s", __FUNCTION__);
    NSError *error = nil;
    NSMutableDictionary* json = nil;

    if(nil != data)
    {
        json = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
    }


    if (error || !json)
    {
        NSLog(@"Could not parse loaded json with error:%@", error);

    } else {
        dispatch_async(dispatch_get_main_queue(), ^{
            allRoutesArray = [json valueForKey:@"Routes"];
            NSLog(@"allRoutesArray count: %lu", (unsigned long)allRoutesArray.count);
            [self.tableView reloadData];
         });
    }

}
ICL1901
  • 7,632
  • 14
  • 90
  • 138
  • I can't see anything obvious; however you only consider HTTP response code 404 to be an error, which is wrong. – trojanfoe Jul 10 '15 at 10:26
  • Thanks.. how is it wrong, and what should I do? – ICL1901 Jul 10 '15 at 10:27
  • 2
    You can only process the data if the status code is 200. Every other status code is an error. – trojanfoe Jul 10 '15 at 10:29
  • Understood.. Thanks a lot.. I'm getting a better response using `NSJSONReadingAllowFragments` rather than `NSJSONReadingMutableContainers` but I don't understand why. – ICL1901 Jul 10 '15 at 10:31
  • You need to examine the data to see if it's valid JSON. – trojanfoe Jul 10 '15 at 10:33
  • I use the path string as a URL and run it in a browser .. The results look fine.. Should I do something else to check validity? Also, it works, it's just sloooow. – ICL1901 Jul 10 '15 at 10:36
  • These NSLog statements likely take more time than everything else does. And I would hope you are aware that a URL request takes as long as it takes, depending on your network speed, server load and lots of other things, and has nothing to do with the code that you are using. – gnasher729 Jul 10 '15 at 11:02
  • 1
    @DavidDelMonte: Do you want to allow JSON fragments? If your response is not a dictionary or array but just a string "Hello", are you prepared to process that? If not, don't use NSJSONReadingAllowFragments. Do you want to modify the result that you get from the parser? If not, don't use NSJSONReadingMutableContainers. You need to understand what these things are doing, not just blindly try them. Every single URL call can take a different time, without any reason. It's not up to your code and your device, it's all the network and the server that determine how long it takes. – gnasher729 Jul 10 '15 at 11:04
  • @trojanfoe: And status 404 is in many cases not an error, but some valuable information that needs to be handled. – gnasher729 Jul 10 '15 at 11:07
  • @gnasher729 I didn't say otherwise. – trojanfoe Jul 10 '15 at 11:19
  • @gnasher729: I just need to read the JSON, and add parts into an NSArray. What URL call should I use to accomplish this? – ICL1901 Jul 10 '15 at 12:09

0 Answers0