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/