create folder inside nsoperation failed

146 Views Asked by At

I use this method inside an NSOperation for check and create a folder:

- (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);
    }

}

Using NSOperation I get this error: Error Domain=NSCocoaErrorDomain Code=512 "The operation couldn’t be completed.

if I don't use NSOperation all work perfectly, this is the 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"];
                               }
                           }];
}

And this the method for create the queue:

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

}
1

There are 1 best solutions below

2
On

You can try something like this:

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];

It worked for me, and I am able to download a file in MyFolder. Hope it works for you. Please let me know if it works.