开发者

Xcode: iPhone Freezes when calling a "sendSynchronousRequest"

I have an issue and don't know how to fix it,

In my app i have a UIWebView that loads pdf's starting by "folder1/1.pdf"

the loading itself works开发者_如何学JAVA just fine but if i call a function in my app that checks if i have a 2.pdf in the same folder, it just freezes, no errors no nothing.

the function i call is:

- (void)isValidURL
{

    int i = 1;
    NSString *folder = @"folder1";
    NSURL *url = [NSURL URLWithString:[baseUrl stringByAppendingFormat:@" %@/%d.pdf",folder,i]];
    NSURLRequest *req = [NSURLRequest requestWithURL:url];
    NSHTTPURLResponse *res = nil;
    NSError *err = nil;
    [NSURLConnection sendSynchronousRequest:req returningResponse:&res error:&err];
}  

Any help is greatly appreciated!

THANKS!!


Synchronous requests freeze the UI while performing their data transfer, and if there's a problem they can hang the UI indefinitely.

If you're just checking a local folder for a file you most definitely don't need to use an NSURLRequest, that's serious overkill and the wrong tool for the job.

You want to use NSFileManager's fileExistsAtPath: method. Much easier. NSURLRequest is for looking out across the network.


Edited to Add

If you're saying that the file is actually across the network then you want to make an asynchronous request so the UI can continue to function, even if it's just displaying a spinning activity indicator. You can use NSURLConnection's initWithRequest and set up a delegate and such, or you can use the excellent ASIHTTPRequest library that does almost all of the work.


You can find a better demonstration in TopPaid sample app of Apple. In this project they explain well how to deal with image downloading by sending an asynchronous request without locking User Interface. In your case you can download the file and save it to file system as a PDF or anything else you want.


I think You can write easily in your own an async functions, it will take small amount a code. See in Apple dev docs about async URL request.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜