1

I am using AFHTTPRequestOperation to download a file from remote server now my problem is AFHTTPRequestOperation starts to download same request multiple time so i want to prevent the download process to execute for same request. so basically what i want is however if downloadFile: function call multiple time with same request i can prevent the download process to start if any process running with same request already. following is my code

AFHTTPRequestOperation *operation;
-(void)downloadFile:(NSURL *)videoUrl{


    NSURLRequest *request = [NSURLRequest requestWithURL:videoUrl];


   operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];


    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
    NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:VIDEO_DIRECTORY_NAME];
    NSString *downloadPath = [dataPath stringByAppendingPathComponent:[NSString stringWithFormat:@"TEMP_%@",[videoUrl lastPathComponent]]];
    NSString *fullPath = [dataPath stringByAppendingPathComponent:[videoUrl lastPathComponent]];

   // NSLog(@"Full Path For Download Video %@",fullPath);
    NSError *error=[[NSError alloc]init];

    if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
        [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error]; //Create folder

    BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:fullPath];

    if (!fileExists)
    {
        NSLog(@"Full Path For Download Video  Started%@",fullPath);
        [operation setOutputStream:[NSOutputStream outputStreamToFileAtPath:downloadPath append:NO]];

        [operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
           // NSLog(@"bytesRead: %lu, totalBytesRead: %lld, totalBytesExpectedToRead: %lld", (unsigned long)bytesRead, totalBytesRead, totalBytesExpectedToRead);
        }];

        [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
             NSError *error;
            if (error) {
                NSLog(@"ERR: %@", [error description]);
            } else {
                CustomAlertView *alert=[[CustomAlertView alloc]init];
                NSUserDefaults *userDefaults=[NSUserDefaults standardUserDefaults];

                //check for Available Space
                if([ALDisk freeDiskSpaceInBytes] >= [operation.response expectedContentLength])
                {


                NSLog(@"Content-lent: %lld", [operation.response expectedContentLength]);
                [alert ShowNotificationInParentView:nil WithTitle:NSLocalizedString(@"Video_title", nil) Message:NSLocalizedString(@"Video_downloaded_succesfully", nil) IsSuperUser:[userDefaults boolForKey:IS_SUPER_USER] TypeOfNotification:ALERT_TYPE_ERROR  IsLoggedIn:YES];
                [assetManager.assetManagerDelegate didAssetManagerSucceedObject:nil ErrorCode:@"" Result:YES ResponseId:VIDEO_DOWNLOAD_RESPONSE_ID];
                }

                else {
                    [alert ShowNotificationInParentView:nil WithTitle:@"Memory Full" Message:NSLocalizedString(@"video_cannot_be_downloaded", nil) IsSuperUser:[userDefaults boolForKey:IS_SUPER_USER] TypeOfNotification:ALERT_TYPE_ERROR  IsLoggedIn:YES];
                    [assetManager.assetManagerDelegate didAssetManagerFailedResponseWithError:[NSError errorWithDomain:@"Memory Full" code:[MEMORY_OUT_OF_STORAGE intValue] userInfo:nil]];

                }
            }


        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"ERR: %@", [error description]);
        [assetManager.assetManagerDelegate didAssetManagerFailedResponseWithError:error];
        }];

        [operation start];
    }


}

Please guide me with any suggestion or solution.

BhavikKama
  • 8,566
  • 12
  • 94
  • 164

0 Answers0