开发者

problem in downloading multiple songs in tabBar Application

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 t开发者_StackOverflow社区o 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


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜