0

How do I retrieve the results from my NSURL file? In this example, in the completionHandler, my results are place in a file called *localfile. I am then parsing the local file with NSXMLParserDelegate in a separate NSObject. Everything works great. however I want and need to the the results of the array _storeProductArray for use elsewhere.

For example, the last NSLog(@"_storeProductArray %@", _storeProductArray); is empty... I would like to see the results outside of the NSURLSession thread.

Thanks

    NSDate *startDate1 = [NSDate date];

    NSURL *storeProductURL = [NSURL URLWithString:storeProductLookupResult];

    NSURLRequest *urlRequest                 = [NSURLRequest requestWithURL:storeProductURL];

    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    NSURLSession *session                    = [NSURLSession sessionWithConfiguration:configuration];
    NSURLSessionDownloadTask *task           = [session downloadTaskWithRequest:urlRequest
                                                              completionHandler:^(NSURL *localfile, NSURLResponse *response, NSError *error)
                                                {
                                                    if (!error)
                                                    {
                                                        if ([urlRequest.URL isEqual:storeProductURL])
                                                        {
                                                            [productXMLQuery parseDocumentWithURL:localfile];

                                                            _storeProductArray = [productXMLQuery productArray];

                                                            NSDate *endDate1 = [NSDate date];

                                                            NSTimeInterval interval1 = [startDate1 timeIntervalSinceDate:endDate1];
                                                            NSLog(@"1 - Time took: %f", interval1);

                                                            NSLog(@"_storeProductArray %@", _storeProductArray);
                                                        }
                                                    }
                                                } ];
    [task resume];

    NSLog(@"_storeProductArray %@", _storeProductArray);
rihekopo
  • 3,241
  • 4
  • 34
  • 63
Paul S.
  • 1,342
  • 4
  • 22
  • 43
  • Is _storeProductArray in your interface? – user1947561 Jul 10 '14 at 15:44
  • Yes, @property NSMutableArray *storeProductArray; – Paul S. Jul 10 '14 at 15:53
  • Do you ever initialize the array? – user1947561 Jul 10 '14 at 15:54
  • Yes of corse, otherwise the NSLog in completionNHandler wouldn't work. I initilziae it in ViewDidLoad _storeProductArray = [NSMutableArray new]; – Paul S. Jul 10 '14 at 16:03
  • Sorry, just double checking so it's not a simple error. Before the request, try `__block NSArray *array = [NSArray new]; // or some variable name`. In the block, set array to the value, then after the block, set _storeProductArray to the blockArray variable. – user1947561 Jul 10 '14 at 16:05
  • I added the following: __block NSMutableArray *resultsArray = [NSMutableArray new]; just before the NSDate above. In the completionHandler I added resultsArray = [productXMLQuery productArray]; after [task resume]; I added _storeProductArray = [NSMutableArray arrayWithArray:resultsArray]; NSLog(@"_storeProductArray %@", _storeProductArray); results are: 2014-07-10 16:00:45.879 POC[3351:149736] _storeProductArray ( ) – Paul S. Jul 10 '14 at 20:02
  • @PaulS. Did you resolve the issue? Unfortunately, I am stuck with the same and is trying to find a way to retrieve the data in this async request. – raurora Jul 31 '14 at 16:10

0 Answers0