【问题标题】:create folder inside nsoperation failed在 nsoperation 中创建文件夹失败
【发布时间】:2014-01-09 21:27:30
【问题描述】:

我在 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 我得到这个错误: Error 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"];
                               }
                           }];
}

这是创建队列的方法:

    for (NSDictionary *dict in fileDaScaricare) {
    DownloadOperation *downloadOperation = [[DownloadOperation alloc] initWithDictionary:dict];
    [self.operationQueue addOperation:downloadOperation];

}

【问题讨论】:

    标签: nsoperation nsoperationqueue create-directory


    【解决方案1】:

    你可以试试这样的:

    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 中下载文件。希望对你有效。请让我知道它是否有效。

    【讨论】:

    • 问题不是代码而是ns操作,经过一些测试,我决定在操作开始之前创建文件夹,现在可以正常工作了!m!
    • 当我在 nsoperation 子类的 main() 方法中使用类似的东西时,它对我来说效果很好。问题可能是由于目录路径。如果你还想在 main() 中尝试,也可以参考这个链接:stackoverflow.com/questions/14248757/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-30
    • 1970-01-01
    • 2017-10-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多