{"action":"created","issue":{"url":"https://api.github.com/repos/AFNetworking/AFNetworking/issues/657","labels_url":"https://api.github.com/repos/AFNetworking/AFNetworking/issues/657/labels{/name}","comments_url":"https://api.github.com/repos/AFNetworking/AFNetworking/issues/657/comments","events_url":"https://api.github.com/repos/AFNetworking/AFNetworking/issues/657/events","html_url":"https://github.com/AFNetworking/AFNetworking/issues/657","id":8598879,"number":657,"title":"AFNetworking + queue + cancel operations + junk files","user":{"login":"zaabalonso","id":1202016,"avatar_url":"https://avatars.githubusercontent.com/u/1202016?v=3","gravatar_id":"","url":"https://api.github.com/users/zaabalonso","html_url":"https://github.com/zaabalonso","followers_url":"https://api.github.com/users/zaabalonso/followers","following_url":"https://api.github.com/users/zaabalonso/following{/other_user}","gists_url":"https://api.github.com/users/zaabalonso/gists{/gist_id}","starred_url":"https://api.github.com/users/zaabalonso/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/zaabalonso/subscriptions","organizations_url":"https://api.github.com/users/zaabalonso/orgs","repos_url":"https://api.github.com/users/zaabalonso/repos","events_url":"https://api.github.com/users/zaabalonso/events{/privacy}","received_events_url":"https://api.github.com/users/zaabalonso/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":8,"created_at":"2012-11-23T09:35:30Z","updated_at":"2015-04-19T09:50:32Z","closed_at":"2012-11-24T02:40:02Z","body":"i 'm downloading some files using AFNetworking using a queue. Here is my code:\n\n\n          apiClient =[[AFHTTPClient alloc]initWithBaseURL: [NSURL URLWithString:ZFREMOTEHOST]];\n    for (NSString *element in self.productsArray) {\n            NSURL *url = [NSURL URLWithString:element];\n            NSURLRequest *request = [NSURLRequest requestWithURL:url];\n\n            op = [[AFHTTPRequestOperation alloc] initWithRequest:request];\n\n            NSString *documentsDirectory = nil;\n            NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);\n            documentsDirectory = [paths objectAtIndex:0];\n\n            NSString *targetFilename = [url lastPathComponent];\n            NSString *targetPath = [documentsDirectory stringByAppendingPathComponent:targetFilename];\n\n            op.outputStream = [NSOutputStream outputStreamToFileAtPath:targetPath append:NO];\n\n            [op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {\n\n            } failure:^(AFHTTPRequestOperation *operation, NSError *error) {\n                //failure case\n                NSLog(@\"BaaZ  File NOT Saved %@\", targetPath);\n\n                //remove the file if saved a part of it!\n                NSFileManager *fileManager = [NSFileManager defaultManager];\n                [fileManager removeItemAtPath:targetPath error:&error];\n\n                if (error) {\n                    NSLog(@\"error dude\");\n                }\n\n                if ([operation isCancelled]) {\n                    //that doesn't work.\n                    NSLog(@\"Canceled\");\n                }\n            }];\n\n            [op setDownloadProgressBlock:^(NSInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {\n                if (totalBytesExpectedToRead > 0) {\n                    dispatch_async(dispatch_get_main_queue(), ^{\n                        self.progressView.alpha = 1;\n                        self.progressView.progress = (float)totalBytesRead / (float)totalBytesExpectedToRead;\n                        NSString *label = [NSString stringWithFormat:@\"Downloaded %lld of %lld bytes\", totalBytesRead,totalBytesExpectedToRead];\n                        self.progressLabel.text = label;\n                    });\n                }\n            }];\n        [self.resourcesArray addObject:op];\n\n        }\n    for (AFHTTPRequestOperation *zabols in self.resourcesArray) {\n        [apiClient.operationQueue addOperation:zabols];\n    }\n\n\nthe code is working fine on file downloading but i want some cancel functionality so i have a button with an action that has the code below:\n\n\n <addr>   [apiClient.operationQueue cancelAllOperations];\n\n\nthe operations cancel file but then there are some junk files on the Documents folder. By saying junk i mean file that started downloading i canceled them and the i get a file with the same name but useless can't be opened cause it's damaged.\n\nHow can i prevent AF from doing that and keep only the completed files when i cancel it?\n\nany help would be grateful.\n\nAlso tried canceling job by job like that:\n\n\n\n    for (AFHTTPRequestOperation *ap in apiClient.operationQueue.operations) {\n            [ap cancel];\n            NSLog(@\"%@\",ap.responseFilePath);\n            NSFileManager *fileManager = [NSFileManager defaultManager];\n            [fileManager removeItemAtPath:ap.responseFilePath error:nil];\n\n        }\n\n\n\nand deleting the junk files but that doesn't work either.\n\n\n\nMY THOUGHTS :\n\n> THERE SHOULD BE SOME KIND OF TEMP_FILES FOLDER AND ON_COMPLETE ACTION MOVE THEM IN THE ACTUAL FILE PATH ELSE DELETE THEM(WE DON\"T NEED SEMI DOWNLOADED FILES).\n\n\nSO i GUESS the problem is here :  op.outputStream = [NSOutputStream outputStreamToFileAtPath:targetPath append:NO];\n\n\nand the way AF manages its downloaded files."},"comment":{"url":"https://api.github.com/repos/AFNetworking/AFNetworking/issues/comments/94256991","html_url":"https://github.com/AFNetworking/AFNetworking/issues/657#issuecomment-94256991","issue_url":"https://api.github.com/repos/AFNetworking/AFNetworking/issues/657","id":94256991,"user":{"login":"dharaahuja","id":584915,"avatar_url":"https://avatars.githubusercontent.com/u/584915?v=3","gravatar_id":"","url":"https://api.github.com/users/dharaahuja","html_url":"https://github.com/dharaahuja","followers_url":"https://api.github.com/users/dharaahuja/followers","following_url":"https://api.github.com/users/dharaahuja/following{/other_user}","gists_url":"https://api.github.com/users/dharaahuja/gists{/gist_id}","starred_url":"https://api.github.com/users/dharaahuja/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dharaahuja/subscriptions","organizations_url":"https://api.github.com/users/dharaahuja/orgs","repos_url":"https://api.github.com/users/dharaahuja/repos","events_url":"https://api.github.com/users/dharaahuja/events{/privacy}","received_events_url":"https://api.github.com/users/dharaahuja/received_events","type":"User","site_admin":false},"created_at":"2015-04-19T09:50:32Z","updated_at":"2015-04-19T09:50:32Z","body":"@implementation UploadOperation\r\n\r\n -(void)backgroundStartProcess\r\n{\r\nAFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];\r\n    \r\n    NSString *libDir = [ZipUploader getLibraryDirectoryPath];\r\n request = [httpClient multipartFormRequestWithMethod:@\"POST\" path:methodName parameters:nil\r\n                               constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {\r\n                                   \r\n                                   [formData appendPartWithFileData:videoData name:@\"motordata\" fileName:[NSString stringWithFormat:@\"%@.tar.bz2\",self.serialNumber] mimeType:@\"application/zip\"];\r\n                                   [formData appendPartWithFormData:[comments dataUsingEncoding:NSUTF8StringEncoding] name:@\"comments\"];\r\n                                   \r\n }];\r\n    \r\n    \r\n    \r\n    [request setTimeoutInterval:20.0];\r\nself.operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];\r\n    \r\n    \r\n    \r\n    //handle progress\r\n    [operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {\r\n        \r\n        if (operation.cancelled) {\r\n            [operation cancel];\r\n            \r\n            [operation pause];\r\n        }\r\n        NSLog(@\"Sent %lld of %lld bytes\", totalBytesWritten, totalBytesExpectedToWrite);\r\n    }];\r\n  [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {\r\n        [shared hideLoadingHUD];\r\n        \r\n  if (self.operationCompletion)\r\n            {\r\n                self.operationCompletion();\r\n            }\r\n       \r\n        \r\n        \r\n    } failure:^(AFHTTPRequestOperation *operation, NSError *error)\r\n     {\r\n         NSLog(@\"upload failed\");\r\n         dispatch_async( dispatch_get_main_queue(), ^{\r\n             \r\n             iToast *toast = [iToast makeText:[NSString stringWithFormat:@\"%@\",[error localizedDescription]]];\r\n             toast.theSettings.gravity = iToastGravityBottom;\r\n             [toast show];\r\n             \r\n             });\r\n             \r\n             if (self.operationCompletion)\r\n             {\r\n                 self.operationCompletion();\r\n             }\r\n         \r\n         \r\n     }];\r\n    if (self.operationstart)\r\n    {\r\n        self.operationstart(operation);\r\n    }\r\n    \r\n    \r\n    \r\n    [operation start];\r\n    \r\n}\r\n\r\n- (BOOL)isConcurrent {\r\n    \r\n    return YES;\r\n    \r\n}\r\n\r\n\r\n\r\n- (BOOL)isExecuting {\r\n    \r\n    return executing;\r\n    \r\n}\r\n\r\n\r\n\r\n- (BOOL)isFinished {\r\n    \r\n    return finished;\r\n    \r\n}\r\n\r\n- (void)main {\r\n    \r\n    if ([self isCancelled]) { return; }\r\n    [self backgroundStartProcess];\r\n    \r\n   \r\n}\r\n\r\n\r\n\r\n-(id)initWithData:(MotorMeasurement*)measurementD fromBatchUpload:(BOOL)batch batchCount:(NSUInteger)batchCoun serialNumber:(NSString*)serial\r\n{\r\n    if (![super init]) return nil;\r\n    self.measurementDict = measurementD;\r\n    self.fromBatchUpload = batch;\r\n    self.batchCount = batchCoun;\r\n    self.serialNumber = serial;\r\n    return self;\r\n}\r\n\r\n-(void)cancel\r\n{\r\n    [super cancel];\r\n    \r\n    [self.operation cancel];\r\n}\r\n\r\n@end\r\n\r\n\r\n@implementation ZipUploader\r\n UploadOperation *upload = [[UploadOperation alloc] initWithData:measureDict fromBatchUpload:self.fromBatchUpload batchCount:self.batchCount serialNumber:motor.SerialNumber];\r\n                [self.operationQueue addOperation:upload];\r\n              \r\n                self.operationCount++;\r\n\r\n\r\n@end\r\n\r\n\r\nHello is there any update to cancel an operation in an operation initiated from main of an NSOperation\r\n\r\nThanks\r\nDhara"}}