Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

newtoiphonesdk

macrumors 6502a
Original poster
Jul 30, 2010
567
2
I have this bit of code to download an mp3 from the server. All is set up with Table View parsing a podcast, and the link for the mp3 is the _entry.articleURL. After just a few minutes, the iPhone kills the connection, and I end up with just a small portion of the mp3 downloaded. Any ideas what may be causing this?
Code:
    -(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;
    }
The issue is that it seems the connectionDidFinishLoading keeps getting called, even if incomplete. Any thoughts?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.