Pobieram niektóre pliki za pomocą AFNetworking za pomocą kolejki. Tu jest mój kodu:AFNetworking + kolejka + operacje anulowania + niepotrzebne pliki
apiClient =[[AFHTTPClient alloc]initWithBaseURL: [NSURL URLWithString:ZFREMOTEHOST]];
for (NSString *element in self.productsArray) {
NSURL *url = [NSURL URLWithString:element];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
op = [[AFHTTPRequestOperation alloc] initWithRequest:request];
NSString *documentsDirectory = nil;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
documentsDirectory = [paths objectAtIndex:0];
NSString *targetFilename = [url lastPathComponent];
NSString *targetPath = [documentsDirectory stringByAppendingPathComponent:targetFilename];
op.outputStream = [NSOutputStream outputStreamToFileAtPath:targetPath append:NO];
[op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//failure case
NSLog(@"BaaZ File NOT Saved %@", targetPath);
//remove the file if saved a part of it!
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager removeItemAtPath:targetPath error:&error];
if (error) {
NSLog(@"error dude");
}
if ([operation isCancelled]) {
//that doesn't work.
NSLog(@"Canceled");
}
}];
[op setDownloadProgressBlock:^(NSInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
if (totalBytesExpectedToRead > 0) {
dispatch_async(dispatch_get_main_queue(), ^{
self.progressView.alpha = 1;
self.progressView.progress = (float)totalBytesRead/(float)totalBytesExpectedToRead;
NSString *label = [NSString stringWithFormat:@"Downloaded %lld of %lld bytes", totalBytesRead,totalBytesExpectedToRead];
self.progressLabel.text = label;
});
}
}];
[self.resourcesArray addObject:op];
}
for (AFHTTPRequestOperation *zabols in self.resourcesArray) {
[apiClient.operationQueue addOperation:zabols];
}
kod działa poprawnie na pobieranie plików, ale chcę trochę anulować funkcjonalność więc mam przycisku z działaniem, które ma poniższy kod:
[apiClient.operationQueue cancelAllOperations];
operacje odwołania plik, ale w folderze Dokumenty znajdują się niepotrzebne pliki. Mówiąc "śmieci" mam na myśli plik, który zaczął pobierać, anulowałem je i otrzymałem plik o tej samej nazwie, ale nie można go otworzyć, ponieważ jest uszkodzony.
W jaki sposób mogę zapobiec AF i zachować tylko ukończone pliki, gdy anuluję?
każda pomoc byłaby wdzięczna.
próbował również anulowanie zadania przez pracę tak:
for (AFHTTPRequestOperation *ap in apiClient.operationQueue.operations) {
[ap cancel];
NSLog(@"%@",ap.responseFilePath);
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager removeItemAtPath:ap.responseFilePath error:nil];
}
i usuwając niepotrzebne pliki, ale to nie działa.
chcesz junks zostać usunięte lub całkowicie pobierane pliki nie są uszkodzone? –
tak dokładnie próbowałem również z własnością isFinished, ale kontynuowałem pobieranie ich, nawet jeśli je anulowałem! – zaabalonso