views:

506

answers:

2

hi all,

I encounter a problem by following your comment. I would like to download different file at same time with different delegate:

.h:

NSMutableData *fileData;

.m:

NSString *imgfile = [NSString stringWithFormat:@"http://xxxx/01.jpg"];
NSURL *fileURL1 = [NSURL URLWithString:imgfile];

NSString *audiofile = [NSString stringWithFormat:@"http://xxxx/01.mp3"];
NSURL *fileURL2 = [NSURL URLWithString:audiofile];

NSURLRequest *request1 = [NSURLRequest requestWithURL:fileURL1 cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.0 ];
NSURLRequest *request2 = [NSURLRequest requestWithURL:fileURL2 cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.0 ];

NSArray *connections = [[NSArray alloc] initWithObjects:
[[NSURLConnection alloc] initWithRequest:request1 delegate:self ],
[[NSURLConnection alloc] initWithRequest:request2 delegate:self ],
nil];

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    fileData = [[NSMutableData alloc] init];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [fileData appendData:data];        
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    NSLog(@"Unable to fetch data");
}

ok, the download process works, but, the file size of jpg and mp3 are incorrect, the only correct thing is the total file size (jpg+mp3), please could you have a look on the code, what is missing?

Another question is, I put the file in a NSMutableArray, my question is, how to check which index of array is the correct file type (jpg and mp3)? because I need to save them to the device folder.

+1  A: 

It looks like both your connections write to the same fileData object and all your problems follow from that.
How to deal with multiple connections you can see relevant question asked here on SO, there's also nice blog post containing NSURLConnection subclass that addresses this issue.

Vladimir
hi Vladimir, thank you really much for the reply, I am almost there, but I encountered a problem, my app is UITableView based, I do not know how to implement :@interface CustomURLConnection : NSURLConnection {NSString *tag;}to my app:@interface LessonSubViewController : UITableViewController{}@endbecause there is a warning message says 'UITableViewController' may not respond to '-initWithRequest:delegate:startImmediately:'and this causes the app crash, how can I solve it?
Georg
Implement it as a separate class inherited from NSURLConnection and then use its instance inside your LessonSubViewController methods.
Vladimir
THANK YOU REALLY MUCH!! I finally overcome this issue! and learned a lesson :)
Georg
hi Vladimir, I run into a sporadical problem, the data I received and saved to device was sometimes unable to be played(mp3) or displayed(jpg), I think there shall be something wrong at the data handling process because I didn't use "tag" to determine the actual data received, could you show some sample code to me? thanks a bunch
Georg
Sorry, its not clear what the problem is...Sample code is in the links I posted in answer. If you run your app on simulator you can browse to files you get and actually see if they are valid.
Vladimir
A: 

ok, here is what I am doing, I set a counter to identify the file type to save:

  • (void)connectionDidFinishLoading:(NSURLConnection *)connection { NSMutableData dataForConnection = [self dataForConnection:(CustomURLConnection)connection]; [connection release];

    // Do something with the dataForConnection.

downloadCount ++; // Copy Image Data to ur location using FileManager //UIImage *img = [UIImage imageWithData:fileData]; NSString *saveDir; if (downloadCount ==1) saveDir = [NSString stringWithFormat:@"001.jpg"]; if (downloadCount == 2) saveDir = [NSString stringWithFormat:@"001.mp3"];

//NSLog(@"saveDir=%@",saveDir);
NSArray  * sysPaths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES );
NSString * filePath = [[NSString alloc] initWithFormat: @"%@/%@", [sysPaths objectAtIndex: 0], saveDir];
NSLog(@"filePath=%@",filePath);

if([dataForConnection writeToFile:filePath atomically:YES])
    NSLog(@"DONE COPYING");
else
    NSLog(@"Write to device error");
//[fileData setLength:0];

if (downloadCount == 2)
{
    downloadCount = 0;
    play the MP3 and display the image file;
}

}

even I run the app on simulator, the file was download without error reported, but sometimes the file can not be opened, even I tried to open it from the folder, I was told the file was corrupted, I do not understand what was wrong, hope you do understand what I am saying.

Georg
SO works not like forum - if you have some extra information you should edit your initial question and post it there (or as a new question), but do not put it as an answer
Vladimir
sorry about that, because the comment has character limitation..so do you read my question? I have been struggling on this issue for several days
Georg
It seems that you rely on fact that 1st connection finished will be jpg download, 2nd - mp3, but sometimes it is not so. Correct way to handle multiple connections must be the following: when you create connection you set a tag to it (say, kJPGConnection for downloading jpg, kMP3Connection - for mp3). When connection finished you check its tag and do corresponding action: if (connection.tag == kJPGConnection) - save as jpg.
Vladimir
Thank you for the hint, I've solved the problem, the app runs very smooth!!
Georg