PDA

View Full Version : Verify file on web




larswik
Feb 25, 2012, 11:25 AM
I am downloading images from my server. Some clients might have 3 images and another might have 1. When the app starts I have an NSThread that runs in the background loading the images. All the image have the same name like image1.jpg, image2.jpg and so on.

I wrote some code that would check the link for the file and if it returned 'nil' it would skip the file. The NSURL is not working like I was hoping. Even if the file is not in the clients folder it still comes back as true. It firsts checks to see if the file is local already.



photoNameArray = [NSArray arrayWithObjects:@"image1.jpg",@"image2.jpg",@"image3.jpg", nil];

for (int x = 0; x < [photoNameArray count]; x++) {
NSString *testForLocalImageFileAtPath = [NSString stringWithFormat:@"%@/clients/%@/%@",[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject],nameWithWhiteSpacesRemoved, [photoNameArray objectAtIndex:x]]; //creates a string path for the image files localy, it does not check the path, just the string.
//NSLog(@"test for image %@",testForLocalImageFileAtPath);

if (![fileManager fileExistsAtPath:testForLocalImageFileAtPath]){

NSString *pathToImages = [NSString stringWithFormat:@"http://www...........com/app/clients/%@/images/photos/%@", nameWithWhiteSpacesRemoved, [photoNameArray objectAtIndex:x]]; //if file is not local, get it on the internet.
NSURL *checkURL = [[NSURL alloc] initWithString:pathToImages];// check for file first
NSLog(@"path to URL %@", testForLocalFileAtPath);
NSLog(@"the URL %@", checkURL);

if (checkURL != nil) {
UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:pathToImages]]]; //Loads the file from the internet to a UIImage

[self saveImageToDir:image withDirectoryName:nameWithWhiteSpacesRemoved withImageNumber:x]; //send message to method to to save the image to a local directory.

}
}
}

I am giving the NSURL the address where the file should be if it exists. Even if it is not there, the IF statement bellow still evaluates to true? What am I missing?



chown33
Feb 25, 2012, 11:45 AM
I'm not sure what you're asking.

If your question is "Why does if (checkURL != nil) evaluate to true?", the answer should be obvious: it's because checkURL is not nil.

This should be obvious because you're obviously assigning a newly alloc'ed and init'ed object to it. So if you're expecting it to be nil, or only sometimes be nil, you should explain the logic of your reasoning process for reaching that conclusion. In short, why did you expect checkURL to be nil? Please explain in the following manner, "I expected checkURL to be nil because ___" and fill in the blank.

If you can't explain the reasoning, then it may be because you've written the wrong code to express whatever you were thinking. Unfortunately, we don't know what you were thinking, all we can see is your code. So you should explain what you're trying to accomplish with the if test in the first place. If you're trying to check for the presence or absence of a web resource, then the code you have is completely wrong.


I recommend that you describe the problem in the following way:
1. Describe what you expected to happen.
2. Describe what actually happened.

In both descriptions, include a description of the output. You expected the following NSLog outputs, and list them. You actually got the following NSLog outputs, and post the actual output.

I keep posting these two points because they aren't just theoretical. They are very practical. They often help clarify the problem in useful ways, both to others (like us) and to yourself. More than once, the process of simply writing the descriptions makes clear there is a logical flaw in my expectations. Once that flaw is plain, I don't even need to ask the question, because I know how to fix the flaw. But if the description is inadequate or poorly thought through, then others can usefully comment on the logic of it.

Without a description of what you expected the code to do, we have no way of knowing what the logical thought process was that resulted in the code. If we don't know what is expected to happen, how could we tell you how to change the code to make that thing happen?

dejo
Feb 25, 2012, 11:52 AM
I am downloading images from my server. Some clients might have 3 images and another might have 1. When the app starts I have an NSThread that runs in the background loading the images. All the image have the same name like image1.jpg, image2.jpg and so on.

I wrote some code that would check the link for the file and if it returned 'nil' it would skip the file. The NSURL is not working like I was hoping. Even if the file is not in the clients folder it still comes back as true. It firsts checks to see if the file is local already.



photoNameArray = [NSArray arrayWithObjects:@"image1.jpg",@"image2.jpg",@"image3.jpg", nil];

for (int x = 0; x < [photoNameArray count]; x++) {
NSString *testForLocalImageFileAtPath = [NSString stringWithFormat:@"%@/clients/%@/%@",[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject],nameWithWhiteSpacesRemoved, [photoNameArray objectAtIndex:x]]; //creates a string path for the image files localy, it does not check the path, just the string.
//NSLog(@"test for image %@",testForLocalImageFileAtPath);

if (![fileManager fileExistsAtPath:testForLocalImageFileAtPath]){

NSString *pathToImages = [NSString stringWithFormat:@"http://www...........com/app/clients/%@/images/photos/%@", nameWithWhiteSpacesRemoved, [photoNameArray objectAtIndex:x]]; //if file is not local, get it on the internet.
NSURL *checkURL = [[NSURL alloc] initWithString:pathToImages];// check for file first
NSLog(@"path to URL %@", testForLocalFileAtPath);
NSLog(@"the URL %@", checkURL);

if (checkURL != nil) {
UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:pathToImages]]]; //Loads the file from the internet to a UIImage

[self saveImageToDir:image withDirectoryName:nameWithWhiteSpacesRemoved withImageNumber:x]; //send message to method to to save the image to a local directory.

}
}
}

I am giving the NSURL the address where the file should be if it exists. Even if it is not there, the IF statement bellow still evaluates to true? What am I missing?

You are not using NSURL's initWithString: properly. It is not intended to check the existence of the URL. You should use another class, probably NSURLConnection's delegate methods.

larswik
Feb 25, 2012, 01:25 PM
Thanks dejo... I had a hunch I was not using that NSURL class correctly for this. I will read up on the NSURLConnection's.

Chown33 - My thought on NSURL *checkURL = [[NSURL alloc] initWithString:pathToImages]; was that I feed it a path to a URL on the web. If that path was a valid path then it would create a valid NSURL object. If there was no such file or path then it would return nil. As I was searching the web I found this answer http://stackoverflow.com/questions/2358344/check-if-valid-url-so-i-can-pass-it-to-an-nsurl which lead me to believe that the object would check to see if it was a valid path I fed it.

When it did not work I could not find the correct object to use so I thought I would ask. dejo provided the NSURLConnection's delegate methods that I will read the doc's on and get it running now I hope.

Thanks.

dejo
Feb 25, 2012, 02:01 PM
And just so you know, URL-validity and whether it references an existing remote resource are two different things.