2014-01-09 52 views
2

我用這個方法的內部的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我得到這個錯誤: 錯誤域= NSCocoaErrorDomain代碼= 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]; 

} 

回答

3

你可以嘗試這樣的事情:

NSString *stringPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,  NSUserDomainMask, YES)objectAtIndex:0]stringByAppendingPathComponent:@"/MyFolder"]; 
NSError *error = nil; 

    if (![[NSFileManager defaultManager] fileExistsAtPath:stringPath]) 
     [[NSFileManager defaultManager] createDirectoryAtPath:stringPath withIntermediateDirectories:NO attributes:nil error:&error]; 

    NSString *docFolderPath = [stringPath stringByAppendingString:[NSString stringWithFormat: @"/%@", self.downloadedFilename]]; 
[data writeToFile:docFolderPath atomically:YES]; 

它的工作對我來說,我能夠下載文件MyFolder文件。希望對你有效。請讓我知道它是否有效。

+0

問題不是代碼而是nsoperation,經過一些測試,我決定在操作啓動之前創建文件夾,現在工作沒有問題! – kikko088

+0

當我在nsoperation子類的main()方法中使用類似的東西時,它對我來說效果很好。該問題可能是由於目錄路徑。如果你仍然想在main()中嘗試,你也可以參考這個鏈接:http://stackoverflow.com/questions/14248757/error-domain-nscocoaerrordomain-code-512-the-operation-couldnt-be-completed-日 –