8

I have tabbar application, in first tab I have a webView, so when user open the website and going to download some song mp3 format, it push another View which takes the title from the user.

after giving the title I just change the tabbarSelectController to one title for song and the downloading begins and shown on the tabbar at selected Index 1. when i change the tab to selected Index 0 and select another song for downloading and again back to selectedIndex 1 first downloading stop and 2nd downloading resume.

so i want to download multiple songs and don't have much idea how to do that in this scenario because user add songs dynamically I have seen ASINtworkQueue as well but don't get the idea of how that works

aimzy
  • 184
  • 2
  • 9

2 Answers2

1

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.

Community
  • 1
  • 1
Can
  • 8,502
  • 48
  • 57
  • nice ans but how we show the progress in progressbar other table in tableView then ?. If the user add one request at one time and that is in download process and he adds another request then what happens ? – dark Sep 01 '11 at 20:35
  • u can download the songgs in background thread,so if another one comes u need to queue it untill first one completed using the nsoperation also......so u need to serialise the operations.. – Linux world Sep 03 '11 at 10:14
  • @lak in Iphone have you some tutorial link of it ?? – dark Sep 03 '11 at 19:18
1

I will suggest it different way..

Try putting a code for song download in different class. Iphone O.S. will automatically start a new connection for each object of that class. Now both downloads can continue at same time.

It works beautyfully. I have seen it.

Aditya Athavale
  • 183
  • 3
  • 12