The ASINetworkQueue
is just a subclass of NSOperationQueue
, what it does, is create a queue of Request objects, so for example, you can have 10 requests waiting, and attend 5 at a time, when a request is completed, another request in the queue becomes active.
Having a queue of requests is certainly helpful in your case, but you haven't pasted any code, on how you're dealing with the requests right now. So I'm gonna give you a "general concept" on how it should be done:
First, I'm assuming that you already figured out how to identify when the user is going to download the song, and have the URL of the file. If not, here's another question related. Also, have ASI installed.
Let's add an object which handles the downloads, say, DownloadManager:
#import "ASINetworkQueue.h"
#import "ASIHTTPRequest.h"
@interface DownloadManager : NSObject <ASIHTTPRequestDelegate>
{
ASINetworkQueue *requestQueue;
}
+ (DownloadManager*)sharedInstance;
- (void)addDownloadRequest:(NSString*)URLString;
I will make this class behave like a singleton (based on this answer), because I imagine that you're working with a single download queue. If this is not the case, adapt it to your needs:
@implementation DownloadManager
static DownloadManager *_shared_instance_download_manager = nil;
+ (DownloadManager*)sharedInstance
{
@synchronize
{
if (!_shared_instance_download_manager)
{
_shared_instance_download_manager = [[DownloadManager alloc] init];
}
return _shared_instance_download_manager
}
}
- (id)init
{
self = [super init];
if (self)
{
requestQueue = [[ASINetworkQueue alloc] init];
requestQueue.maxConcurrentOperationCount = 2;
requestQueue.shouldCancelAllRequestsOnFailure = NO;
}
return self;
}
- (void)addDownloadRequest:(NSString*)URLString
{
NSURL *url = [NSURL URLWithString:URLString];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
request.delegate = self;
[requestQueue addOperation:request];
[requestQueue go];
}
- (void)requestFinished:(ASIHTTPRequest*)request
{
// Request finished.
}
- (void)dealloc
{
[requestQueue release];
[super dealloc];
}
@end
With all that done, now you can then add the download requests simply doing:
DownloadManager *manager = [DownloadManager sharedInstance];
[manager addDownloadRequest:MyUrl];
The first tab would add the items to the DownloadManager
, the other tab would have to hear for when a download is complete, or the current status. I didn't add that into the code, as it's dependant on how you do these things. It could be a custom delegate method (i.e - (void)DownloadManager:(DownloadManager*)downloadManager didFinishDownloadRequest:(ASIHTTPRequest*)request
), or pass the current delegate of the requests, or using NSNotificationCenter.