fdf33
fdf33

Reputation: 73

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.

Upvotes: 0

Views: 1375

Answers (3)

Stefan Arentz
Stefan Arentz

Reputation: 34945

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.

Upvotes: 0

No one in particular
No one in particular

Reputation: 2672

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.

Upvotes: 1

tc.
tc.

Reputation: 33592

NSURLRequest (or NSMutableURLRequest) and NSURLConnection.

Upvotes: 0

Related Questions