0

I've changed my old NSthread because couldn't update UI from a background thread. Now I'm using dispatch_async.

The problem is when this code is executed the second time, in ApplicationDidBecomeActive (when user hide and show again de application) Never exit from that while, keeps in infinite loop.

Here I show the new code about dispatch_async, the NSURLConnection code always worked , resume at the end... I don't think the problem is there

- (void)applicationDidBecomeActive:(UIApplication *)application
   {    

     //  first connection to the server through NSURLConnection, downloading a json list
     [wsDataVideos getVideosData:URL_WS_VIDEOS];

     dispatch_queue_t secondDownloadQueue = dispatch_queue_create("name",NULL);            
     dispatch_async(secondDownloadQueue, ^{

           // wait for wsDataVideos has finished. IT SEEMS THIS COLLAPSES CPU ¿?
           while (wsDataVideos.imported==NO) {}

          // If are there new videos begins sincronous and slower download:
          if ....{

             dispatch_async(dispatch_get_main_queue(), ^{
                // Update the UI: inform user begins download
                [menuViewController.badgeVideos setBadgeVideosText:@"Downloading..."];
                 });

             // Download videos (sincro)

             dispatch_async(dispatch_get_main_queue(), ^{
                 // informs user is completed
                 [menuViewController.badgeVideos setBadgeVideosText:@"Downloaded"];
                 });
           } 
        });

    }

The first connection (json), than I wait in that while :

-(void)getVideosData:(NSString *)url_ws{

    NSLog(@"Get videos data1 ");

    if  (wsData){
        [wsData setLength:0];
    }
    else{
        wsData=[[NSMutableData alloc] init];
    }

    NSURLRequest *reqCat=[NSURLRequest requestWithURL:[NSURL URLWithString:url_ws] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
    NSURLConnection *conCat=[[NSURLConnection alloc] initWithRequest:reqCat delegate:self];

}

with their methods:

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
...
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
    [wsData    appendData:data];
}

...
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
...

Any ideas? thanks again.

Albert Català
  • 2,026
  • 2
  • 29
  • 35
  • What sets wsDataVideos.imported to YES? Is that line of code ever executed? Is the getVideosData: being executed in the main thread (as it appears to be), and synchronously? It appears that that entire method will finish before getting into your async block. – shortstuffsushi Oct 24 '13 at 03:57
  • First connection, through asynchron NSURLConnection sets wsDataVideos to YES, in connectionDidFinishLoading (and coonectionDidFail..error). It Is launched in tha main thread, and yes asynchornously – Albert Català Oct 24 '13 at 10:48
  • Before that code, I had NSThread new thread.. and [thread start] instead of dispatch_async. And worked perfectly despite a Warning (CoreAnimation: http://stackoverflow.com/questions/19544207/how-to-avoid-coreanimation-warning-deleted-thread-with-uncommitted-catransactio) – Albert Català Oct 24 '13 at 10:53
  • I've added first connection code, perhaps the problem is there ¿? [wsDataVideos getVideosData:URL_WS_VIDEOS – Albert Català Oct 24 '13 at 11:02
  • It doesn't appear that you're starting the conCat connection ever. Try adding [conn start]; – shortstuffsushi Oct 24 '13 at 13:23
  • The connection starts automatically when execute: NSURLConnection *conCat=[[NSURLConnection alloc] initWithRequest:reqCat delegate:self]; – Albert Català Oct 25 '13 at 08:35
  • I realized than if i change this "while { wsDataVideos.imported==NO....}" for a "sleep(10)" it works, maybe that "while[..." collapses the CPU – Albert Català Oct 25 '13 at 13:07
  • I've changed the title – Albert Català Oct 25 '13 at 13:29
  • I wasn't aware those started on their own, I was under the impression you had to use a different method for that (something that ends with startImmediately:(BOOL)start). – shortstuffsushi Oct 25 '13 at 18:25

1 Answers1

0

If I put code inside while{} it works, but i think this is somthing odd..chunk, bad code, isn't it? Maybe a better soluttion would be somthing like dispatch_group_wait() ?

   // wait for wsDataVideos has finished. IT SEEMS THIS COLLAPSES CPU ¿?
   while (wsDataVideos.imported==NO) {
        sleep(1)
  }
Albert Català
  • 2,026
  • 2
  • 29
  • 35
  • How many times (roughly) is that being called before completing? Is it completing immediately after sleeping once? If so, I've seen similar behavior when calling dispatch_async(someThread), then dispatch_async(mainThread) immediately. Something about immediately rejoining seems to cause problems, though I haven't really looked into what or why. – shortstuffsushi Oct 25 '13 at 18:27
  • The solution is... Take it easy: dispatch_async for new asynchronous thread, within this thread launch synchronous connections to web service. I wanted to use NSURlconnection with asynchronous... We'll Apple docs says "try not to use synchronous connections" – Albert Català Oct 25 '13 at 20:21
  • Alternatively, you could have your class that does the "getVideosData" use a block or delegate callback on completion rather that using a "waiting" technique. – shortstuffsushi Oct 25 '13 at 22:02