Loading NSMutableDictionary from URL
I'm currently doing this:
NSMutableDictionary *dict = [[NSDictionary alloc] initWithContentsOfURL: [NSURL URLWithString:@"http://mysite/mypage.php"]];
开发者_开发技巧
Which is great, apart from when the data being returned is quite large, and it appears to time out. How could I get around this?
Thanks in advance.
I'm not a big fan of using NSDictionary to manage downloads. I'd probably try something like:
NSURL *url = [NSURL URLWithString:@"http://mysite/mypage.php"];
NSURLRequest *request = [NSURLRequest requestWintURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
Now, if data is not NULL then save to local file. Then load the dictionary using the contents of that file using the initWithContentsOfFile: method.
If you still get the timeouts you can try larger timeoutIntervals.
NSURLRequest (or NSMutableURLRequest) and NSURLConnection.
In convenience methods like initWithContentsOfURL
you have no control over things like timeouts. They are fine in my cases but it sounds like you will need to use the more low-level NSURLConnection
and NSURLRequest
to load data from the server. There are many examples on the net.
精彩评论