Here is my code for downloading an mp3 that is shown in a table view:
I have it set to check if the file already exists, and if a download is already in progress, and if neither are true, then it downloads. The problem is that if during a download I click another row, and get the warning to wait until the current download is finished, it still names the original mp3 with the name of the 2nd clicked row. Any ideas how I can keep this from happening?
Code:
downloadAction:^ {
RSSEntry *entry = [_allEntries objectAtIndex:indexPath.row];
self.nameit = entry.articleTitle;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pdfPath = [documentsDirectory stringByAppendingPathComponent:[nameit stringByAppendingString:@".mp3"]];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:pdfPath];
if (fileExists) {
NSLog(@"You already got that one dummy!");
UIAlertView *ditto = [[UIAlertView alloc] initWithTitle:@"Already Downloaded" message:@"You have already downloaded this sermon." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[ditto show];
[ditto release];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
if (!fileExists) {
if (downloadInProgress) {
UIAlertView *inprogress = [[UIAlertView alloc] initWithTitle:@"Busy" message:@"Please let your previous download finish before starting another." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[inprogress show];
[inprogress release];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
if (!downloadInProgress) {
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];
}
}
}
}