2012-07-17 5 views
1

J'ai ce peu de code pour télécharger un mp3 du serveur. Tout est configuré avec Table View analysant un podcast, et le lien pour le mp3 est le _entry.articleURL. Après seulement quelques minutes, l'iPhone tue la connexion, et je me retrouve avec juste une petite partie du mp3 téléchargé. Des idées de ce qui peut causer cela?NSURL Connexion de l'application iPhone Télécharger Terminer avant d'avoir terminé

-(void)didSelectRowAtIndexPath 
{  RSSEntry *entry = [_allEntries objectAtIndex:indexPath.row]; 

      self.nameit = entry.articleTitle; 
      NSURL *url = [NSURL URLWithString:entry.articleUrl];  
      NSURLRequest *theRequest = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60]; 
      __block NSURLConnection *connection = [NSURLConnection connectionWithRequest:theRequest delegate:self]; 

      UIApplication *application = [UIApplication sharedApplication]; //Get the shared application instance 

      __block UIBackgroundTaskIdentifier background_task; //Create a task object 

      background_task = [application beginBackgroundTaskWithExpirationHandler:^{ 
       // This code gets called when your app has been running in the background too long and the OS decides to kill it 
       // You might want to cancel your connection in this case, that way you won't receive delegate methods any longer. 
       [connection cancel]; 
       [application endBackgroundTask: background_task]; //Tell the system that we are done with the tasks 
       background_task = UIBackgroundTaskInvalid; //Set the task to be invalid 

       //System will be shutting down the app at any point in time now 
      }]; 

      self.backgroundTaskIdentifier = background_task; 
      if (connection) { 
       receivedData = [[NSMutableData data] retain]; 
       self.thetable = tableView; 
       self.thepath = indexPath; 
      } 
      else { 
       UIAlertView *cancelled = [[UIAlertView alloc] initWithTitle:@"Download Failed" message:@"Please check your network settings, and then retry the download." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
       [cancelled show]; 
       [cancelled release]; 
      } 
} 

- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { 
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES; 
progress.hidden = NO; 
downloadInProgress = YES; 
RSSEntry *entry = [_allEntries objectAtIndex:thepath.row]; 

self.nameit = entry.articleTitle; 
downloadlabel.text = [NSString stringWithFormat:@"%@", nameit]; 
[thebar addSubview:downloadlabel]; 
[receivedData setLength:0]; 
expectedBytes = [response expectedContentLength]; 
} 

- (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { 
[receivedData appendData:data]; 
float progressive = (float)[receivedData length]/(float)expectedBytes; 
[progress setProgress:progressive]; 


} 

- (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { 
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO; 
[connection release]; 
UIAlertView *connectionfailed = [[UIAlertView alloc] initWithTitle:@"Download Failed" message:@"Please check your network settings, and then retry the download." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
[connectionfailed show]; 
[connectionfailed release]; 
progress.hidden = YES; 
downloadInProgress = NO; 
[downloadlabel removeFromSuperview]; 
[thetable deselectRowAtIndexPath:thepath animated:YES]; 
[[UIApplication sharedApplication] endBackgroundTask:self.backgroundTaskIdentifier]; 
self.backgroundTaskIdentifier = UIBackgroundTaskInvalid; 
[connection release]; 



} 

- (NSCachedURLResponse *) connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse { 
return nil; 
} 

- (void) connectionDidFinishLoading:(NSURLConnection *)connection { 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 

NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *pdfPath = [documentsDirectory stringByAppendingPathComponent:[nameit stringByAppendingString:@".mp3"]]; 
NSLog(@"Succeeded! Received %d bytes of data",[receivedData length]); 
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO; 
[receivedData writeToFile:pdfPath atomically:YES]; 
progress.hidden = YES; 
downloadInProgress = NO; 
[downloadlabel removeFromSuperview]; 

[thetable deselectRowAtIndexPath:thepath animated:YES]; 

[[UIApplication sharedApplication] endBackgroundTask:self.backgroundTaskIdentifier]; 
self.backgroundTaskIdentifier = UIBackgroundTaskInvalid; 
} 

Le problème est qu'il semble que le connectionDidFinishLoading continue d'être appelé, même s'il est incomplet. Des pensées?

Répondre

1

Vous ne savez pas pourquoi vous utilisez beginBackgroundTaskWithExpirationHandler pour faire ce travail - son but est d'effectuer une tâche complètement différente (faire un peu de travail après le déplacement de votre application en arrière-plan. la raison de vos problèmes.

Ce que je vous suggère de faire est d'aller fouiller sur github etc pour un exemple de projet de démonstration concurrente NSOperation, et les utiliser pour faire vos NSURLConnections asynchrones.

, vous aussi mise à jour d'une interface graphique élément dans une vue dans les rappels Gardez à l'esprit que vous devez être sur le thread principal de travailler avec UIKit.Si vous avez besoin de mettre à jour quelque chose, il suffit d'utiliser un bloc et l'envoyer dans la file d'attente principale pour effectuer la mise à jour.

+0

J'avais ceci dedans pour que le téléchargement puisse continuer même quand en arrière-plan. – user717452

+0

Je fais un usage intensif de blocs et de NSOperations simultanées. Tous les exemples de projets sur github sont plus ou moins les mêmes, les miens font de nombreux commentaires sur la façon d'obtenir le runloop et une configuration de pool autorelease, mais en tout cas ce que vous avez écrit est assez inhabituel Pour que vous puissiez travailler, vous serez seul pour le faire, car je doute que quelqu'un d'autre puisse comprendre tout cela. –

Questions connexes