Question

Okay, in my app, I'm fetching json object that contains urls of some images I need to download. I do all of that with code below, Images are being downloaded and saved into directory I created tmp/Assets/ but NSFileManager doesn't see them until I restart the application. I mean, files are there, I can see them using organizer, just whatever I do filemanager doesn't see them until I start the app again. Is there some kind of refresh function for nsfilemanager?

- (void)updateResizeableAssets{
NSString* tempDirectory = NSTemporaryDirectory();
tempDirectory = [tempDirectory stringByAppendingString:@"Assets/"];
if(![[NSFileManager defaultManager] fileExistsAtPath:tempDirectory isDirectory:nil])
{
    [[NSFileManager defaultManager] createDirectoryAtPath:tempDirectory withIntermediateDirectories:YES attributes:nil error:nil];
}

NSURLRequest *requestResizeable = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://someurl.com/"]];
[NSURLConnection sendAsynchronousRequest:requestResizeable queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData* data, NSError *connectionError){
    NSError *jsonParsingError = nil;
    NSDictionary *arrayOfImages;
    NSArray *object = [NSJSONSerialization JSONObjectWithData:data options:0 error:&jsonParsingError];
    for(int i=0; i<[object count];i++)
    {
        arrayOfImages= [object objectAtIndex:i];
        NSString *imageStringUrl = @"http://someotherurl.com/";
        imageStringUrl = [imageStringUrl stringByAppendingString:(NSString*)[arrayOfImages objectForKey:@"name"]];
        NSURLRequest *imageUrl = [NSURLRequest requestWithURL:[NSURL URLWithString:imageStringUrl]];

        if(![[NSFileManager defaultManager] fileExistsAtPath:[tempDirectory stringByAppendingString:[arrayOfImages objectForKey:@"name"]]])
        {
            [NSURLConnection sendAsynchronousRequest:imageUrl queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData* data, NSError *connectionError){
                [[NSFileManager defaultManager] createFileAtPath:[tempDirectory stringByAppendingString:[arrayOfImages objectForKey:@"name"]] contents:data attributes:Nil];

            }];
        }
        //NSLog(@"Image: %@", [arrayOfImages objectForKey:@"name"]);
    }

    NSString* tempDirectory = NSTemporaryDirectory();
    tempDirectory = [tempDirectory stringByAppendingString:@"Assets/"];
    NSArray *files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:tempDirectory error:nil];
    for(int i=0; i<[files count]; i++)
    {
        //HERE I GOT NOTHING UNTIL I RESTART THE APP (Run App twice)
        NSLog(@"%@", [files objectAtIndex:i]);
    }
}];

}

Was it helpful?

Solution

This is my approach: The code in the block is called asynchronously so when you get to this part:

[NSURLConnection sendAsynchronousRequest:imageUrl queue:[NSOperationQueue mainQueue]   completionHandler:^(NSURLResponse *response, NSData* data, NSError *connectionError){
            [[NSFileManager defaultManager] createFileAtPath:[tempDirectory stringByAppendingString:[arrayOfImages objectForKey:@"name"]] contents:data attributes:Nil];

        }];

The executions continues, which means that the main block is terminating before the second block finish his work.

so put the rest of the code inside your NSURLConnection block:

[NSURLConnection sendAsynchronousRequest:imageUrl queue:[NSOperationQueue mainQueue]   completionHandler:^(NSURLResponse *response, NSData* data, NSError *connectionError){
            [[NSFileManager defaultManager] createFileAtPath:[tempDirectory stringByAppendingString:[arrayOfImages objectForKey:@"name"]] contents:data attributes:Nil];

   NSString* tempDirectory = NSTemporaryDirectory();
   tempDirectory = [tempDirectory stringByAppendingString:@"Assets/"];
   NSArray *files = [[NSFileManager defaultManager]      contentsOfDirectoryAtPath:tempDirectory error:nil];
   for(int i=0; i<[files count]; i++)
   {
       //HERE I GOT NOTHING UNTIL I RESTART THE APP (Run App twice)
       NSLog(@"%@", [files objectAtIndex:i]);
   }
}];

I hope this helps :).

Edited
While working with blocks it can be hard to understand what is happening, the problem is that you are creating the file inside a block and that block is in a for loop so lets say that when you make your first request i=0, then your request can take a while to give you the response, which means your loop will continues( will not wait for your first request) so when i=1 maybe your first request is not done yet, so your first file was not created yet. If your are going to use blocks and you need to wait for the response you will need to call all the request from inside the block lets say a recursive method. I will recomend you to use the delegate methods it can be more work but you have more controll of what is happening. http://codewithchris.com/tutorial-how-to-use-ios-nsurlconnection-by-example/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top