0

I am using AFNetworking to download an mp3 file. The download happens in a particular ViewController. Since the mp3 file is quite large, it takes a few minutes to complete the download. The problem is that when i go to another ViewController, the download stops and i have to stay on the download ViewController and wait for the download to complete. That would be frustrating for the user! Is there a way to get the download going even when the download ViewController is dismissed? Here is the code i use for downloading:

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.sample.com/samplefile.mp3"]];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
NSString *mp3Name = @"sample.mp3";

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:mp3Name];
operation.outputStream = [NSOutputStream outputStreamToFileAtPath:path append:NO];

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"Successfully downloaded file to %@", path);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];
Basheer_CAD
  • 4,908
  • 24
  • 36
devdev101
  • 241
  • 1
  • 5
  • 14

2 Answers2

0

You can create singleton

 +(id)sharedInstance
{
static dispatch_once_t pred;
static MyClass *sharedInstance = nil;
dispatch_once(&pred, ^{
    sharedInstance = [[MyClass alloc] init];
});
return sharedInstance;
}

And implement your downloading operation in it.

If you did not work with singletone before you should read this article iOS design patterns

Also read this article on raywenderlich. It will give you an idea how to organize work with Web services correctly

AFNetworking crash course

NSOperations

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
Roma
  • 1,107
  • 9
  • 19
  • Thanks Roma! I never worked with singletones so im gonna head to the almighty raywenderlich! Will update my answer if i get anything. – devdev101 Feb 15 '14 at 18:25
  • By the way, am i using the right method for downloading? Note that i want to download the file (asynchronously) in background so the user can browse the application while the file downloads. – devdev101 Feb 15 '14 at 18:28
  • As I can see from the code you have posted there - yes)) AFNetworking is good library for working with web services. AFNetworking is using NSOperations which are performing in background threads – Roma Feb 15 '14 at 18:37
0

The simplest way to achieve what you want would be to put a method in the ApplicationDelegate that starts the download and notifies your view controllers once your download is done. So just move your code to the applicationDelegate.

Please note: I don't think thats the best way to do it, it's much better to handle network traffic in a separate class that takes for it, but the easiest way is using the (already established) Singleton that is your ApplicationDelegate.

Blitz
  • 5,521
  • 3
  • 35
  • 53