NSOperationQueue(ASINetworkQueue) issue while uploading in iOS

125 views Asked by At

Using NSOperationQueue to upload multiple files to server.

//Class B

While uploading multiple files to server at the time move to popviewcontroller (class A). App suddenly crashed. Is there any way to uploading a files to server without interrupt.

Thanks in Advance

//Class B

-(void)UploadtoS3
{
    // Convert file to data from locapathfile here
    NSData* imgData = [NSData dataWithContentsOfFile:localFilePath]; //VIDEO FILEPATH OR IMAGEPATH

    if(![self upload_NetworkQueue]) // If Queue is not initialized
    {
        [[self upload_NetworkQueue] cancelAllOperations];
        [self setUpload_NetworkQueue:[ASINetworkQueue queue]];
        [[self upload_NetworkQueue] setDelegate:self];
        [[self upload_NetworkQueue] setRequestDidFailSelector:@selector(upload_RequestFailed:)];
        [[self upload_NetworkQueue] setQueueDidFinishSelector:@selector(upload_RequestDone:)];
        [[self upload_NetworkQueue] setShowAccurateProgress:YES];
        [[self upload_NetworkQueue] setMaxConcurrentOperationCount:1];
    }

    NSString *s3keyPath = [NSString stringWithFormat:@"/test123/%@",fileName];
    NSLog(@"UPLOAD IMAGE S3 FILE NAME -----------  %@",s3keyPath);
    request = [ASIS3ObjectRequest PUTRequestForData:imgData withBucket:testBuck key:s3keyPath];
    [request setSecretAccessKey:s3SecretKey];
    [request setAccessKey:s3AccessKey];
    [request setTimeOutSeconds:20];
    [request setDelegate:self];
    [request setNumberOfTimesToRetryOnTimeout:3];
    [request setMimeType:mimeType];
    [request setUploadProgressDelegate:self];
    [[self upload_NetworkQueue] addOperation:request];
    [[self upload_NetworkQueue] go];
}
1

There are 1 answers

0
Duncan C On

As the other poster suggested, you should move away from ASI's code. It is no longer supported.

As to your problem, though. My guess is that the code you posted is in a view controller. (Class B in your post) You are setting the view controller as the delegate of your networking class.

Then you are popping from the view controller that initiated the download to another view controller. That causes the view controller that requested the uploads to be deallocated. Thus, when the download queue tries to send a notification to it's delegate, you crash.

In general you should not make a view controller manage application global things like downloads. It would be better to create a singleton class that manages your downloads for you. If you're not familiar with the singleton design pattern then do a google search. There are lots of tutorials online explaining how to set up a singleton in iOS/Objective-C.