Я использую этот метод внутри NSOperation для проверки и создать папку:создать папку внутри NSOperation не удалось
- (void) checkAndCreateFolderWithPath:(NSString *)path {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *pathDaControllare = [[self getLibraryPath] stringByAppendingPathComponent:path];
NSError *error = nil;
BOOL isDir;
BOOL exists = [fileManager fileExistsAtPath:pathDaControllare isDirectory:&isDir];
if (exists) {
if (isDir) {
}
}
else {
[fileManager createDirectoryAtPath:pathDaControllare
withIntermediateDirectories:YES
attributes:nil
error:&error];
NSLog(@"%@",error);
}
}
Использование NSOperation я получаю эту ошибку: Ошибка Domain = NSCocoaErrorDomain Code = 512 «Операция не может будет завершено.
, если я не использую NSOperation все работы отлично, это NSOperation
- (void) main {
NSString *filePath = [fileDict objectForKey:@"url"];
NSString *urlStr = [NSString stringWithFormat:@"http://www.allmyapp.net/wp-content/iFormulario_Update/%@",filePath];
urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:urlStr]];
[NSURLConnection sendAsynchronousRequest:urlRequest
queue:[[NSOperationQueue alloc] init]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){
if (data) {
NSString *folderPath = [filePath stringByReplacingOccurrencesOfString:[filePath lastPathComponent] withString:@""];
[self checkAndCreateFolderWithPath:folderPath];
NSString *pathFile = [[self getLibraryPath] stringByAppendingString:filePath];
[data writeToFile:pathFile atomically:YES];
[self addSkipBackupAttributeToItemAtURL:[NSURL URLWithString:pathFile]];
[[NSNotificationCenter defaultCenter] postNotificationName:@"endFile" object:nil];
[self willChangeValueForKey:@"isFinished"];
[self willChangeValueForKey:@"isExecuting"];
isExecuting = NO;
isFinished = YES;
[self didChangeValueForKey:@"isExecuting"];
[self didChangeValueForKey:@"isFinished"];
}
}];
}
И этот метод для CRE ели очередь:
for (NSDictionary *dict in fileDaScaricare) {
DownloadOperation *downloadOperation = [[DownloadOperation alloc] initWithDictionary:dict];
[self.operationQueue addOperation:downloadOperation];
}
проблема не в коде, а в nsoperation, после некоторого теста я решил создать папку перед началом работы и теперь работать без проблем! M! – kikko088
Когда я использовал подобную вещь внутри основного() метода подпроцесса nsoperation, он работал хорошо для меня. Проблема может быть связана с каталогом. Если вы все еще хотите попробовать в main(), вы также можете сослаться на эту ссылку: http://stackoverflow.com/questions/14248757/error-domain-nscocoaerrordomain-code-512-the-operation-couldnt-be-completed- го –