0

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];
}
4

1 回答 1

0

正如另一张海报所建议的那样,您应该远离 ASI 的代码。它不再受支持。

不过,至于你的问题。我的猜测是您发布的代码在视图控制器中。(您的帖子中的 B 类)您将视图控制器设置为网络类的代表。

然后,您将从启动下载的视图控制器弹出到另一个视图控制器。这会导致请求上传的视图控制器被释放。因此,当下载队列尝试向其委托发送通知时,您会崩溃。

一般来说,你不应该让视图控制器管理应用程序全局的东西,比如下载。最好创建一个为您管理下载的单例类。如果您不熟悉单例设计模式,请进行谷歌搜索。网上有很多教程解释了如何在 iOS/Objective-C 中设置单例。

于 2015-03-09T14:04:58.160 回答