0

I'm developing a library that gets json data from a server, and I'm using NSURLSessionDataTask. In order to test my library I created a new project that calls this library method.

typedef void (^CompletionBlock)();
// More stuff...
- (void)downloadAllPodcastsMetadataWithCompletionHandler:(CompletionBlock)block{

   NSURL *url = [NSURL URLWithString:@"MyServerURL"];

   NSURLSessionDataTask *dataTask = [self.session dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
     // Connection and response handling stuff...
     // When my data is saved, executes the block parameter

     dispatch_async(dispatch_get_main_queue(), ^{
        block();
     });
   }
   [dataTask resume];
}

EDIT: I forgot to put [dataTask resume] here, but it's in my code. Sorry.

EDIT 2: So, as some of you have said, i changed dispatch_sync with dispatch_async. But the result is the same :(

In my test project i call this method like this.

[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

[manager downloadAllPodcastsMetadataWithCompletionHandler:^{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}];

But the network activity Indicator never shows off. It's like the block parameter never executes.

It's because it executes the NSURLSessionDataTask logic inside a library and then I should use something else instead dispatch_sync?

I already checked NSURLSessionDataTask not executing the completion handler block and I think I do the same. If it helps, manager is a Singleton. Any thoughts?

Thank you.

Community
  • 1
  • 1
WedgeSparda
  • 1,161
  • 1
  • 15
  • 40
  • I wrote a class that can help you: https://github.com/mattneub/Programming-iOS-Book-Examples/blob/master/bk2ch24p842downloader/ch37p1099downloader/MyDownloader.m – matt Jan 16 '14 at 13:09
  • Never use `dispatch_sync` unless you know exactly what you are doing. You are blocking yourself here! – matt Jan 16 '14 at 13:10
  • Wow, thanks for that class. I'll check it out :) – WedgeSparda Jan 16 '14 at 13:16

4 Answers4

0

You need to start the download with [dataTask resume];.

Rhythmic Fistman
  • 34,352
  • 5
  • 87
  • 159
0

You need to add [dataTask resume]; and also add this in completion handler dispatch_async(dispatch_get_main_queue(), ^{ [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; });

nerowolfe
  • 4,787
  • 3
  • 20
  • 19
  • I forgot to add [dataTask resume]; in the question, but no in my code. Sorry. I put [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; inside the block parameter that receives the method. I dis it this way because it is a library, no a app, so this way can be use in many apps. – WedgeSparda Jan 16 '14 at 13:28
0

I tested some code.

Dispatch_sync won't work.

dispatch_sync(dispatch_get_main_queue(), ^{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
});

Dispatch_async worked for me. But I had to put the code on veiwWillAppear. Not sure what's going to happen using it inside a block...

dispatch_async(dispatch_get_main_queue(), ^{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
});
wolffan
  • 1,084
  • 10
  • 15
  • I changed, but the result it's the same. – WedgeSparda Jan 16 '14 at 13:31
  • Did you read this? http://stackoverflow.com/questions/13586166/issue-with-network-activity-indicator-placement – wolffan Jan 16 '14 at 13:46
  • Yes, sorry. But i think it can't help me :( I'm doing my test right after my teste app is launched. I don't have any view or viewcontroller created. Just showing the network indicator. – WedgeSparda Jan 16 '14 at 14:58
0

Well, i'm dumb.

There was an error in my previous code i didn't notice, so the completion block war never executing. That is what happens when someone has too much confidence in his code.

Sorry for bothering you.

WedgeSparda
  • 1,161
  • 1
  • 15
  • 40