Problem loading multiple tracks in tabBar application

I have a tab application, on the first tab I have a webView, so when a user opens a website and is going to download any mp3 format of the song, he clicks a different view, which takes the title from the user.

after naming it, I just change the tabbarSelectController to one title for the song and the download starts and appears on the tab in the selected Index 1. When I change the tab to the selected index 0 and select another song to download and again return to selectedIndex 1 the first download and the 2nd loading.

so I want to download some songs and don’t really understand how to do this in this scenario, because the user dynamically adds songs, I also saw ASINtworkQueue, but I don’t understand how it works.

+8
iphone ios4 uitabbarcontroller
source share
2 answers

ASINetworkQueue is simply a subclass of NSOperationQueue , what it does is create a queue of Request objects, so for example, you can have 10 pending requests and visit 5 at a time when the request is completed, another request in the queue becomes active.

Having a request queue is certainly useful in your case, but you have not inserted any code, as you are dealing with requests right now. Therefore, I will give you a "general concept" on how to do this:

Firstly, I assume that you have already figured out how to determine when the user will download a song and have the file URL. If not, here is another related question . Also installed ASI .

Add an object that handles downloads, say, DownloadManager:

 #import "ASINetworkQueue.h" #import "ASIHTTPRequest.h" @interface DownloadManager : NSObject <ASIHTTPRequestDelegate> { ASINetworkQueue *requestQueue; } + (DownloadManager*)sharedInstance; - (void)addDownloadRequest:(NSString*)URLString; 

I will create this class as singleton ( based on this answer ) because I assume that you are working with a single load queue. If it is not, 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 this, now you can add download requests simply:

 DownloadManager *manager = [DownloadManager sharedInstance]; [manager addDownloadRequest:MyUrl]; 

The first tab will add elements to the DownloadManager , the other tab should be listened to after the download is completed or the current state. I did not add this to the code as it depends on how you do it. This can be a custom delegate method (i.e. - (void)DownloadManager:(DownloadManager*)downloadManager didFinishDownloadRequest:(ASIHTTPRequest*)request ) or pass the current request delegate or use the NSNotificationCenter .

+1
source share

I suggest this in a different way.

Try putting the code to load the song in a different class. Iphone OS will automatically start a new connection for each object of this class. Both downloads can now continue at the same time.

It works beautifully. I have seen that.

+1
source share

All Articles