2

I would like to better understand the functioning of of initWithContentsOfURL of NSDictionary.
This function manages by itself the failure of a connection?
From the initWithContentsOfURL of NSDictionary reference:

- (id)initWithContentsOfURL:(NSURL *)aURL

Return Value

An initialized dictionary-which Might be different than the original-that contains the receiver at aURL dictionary, or nil if there is an error or if the contents of the resource are an invalid representation of a dictionary.

Ok, but does not specify whether the url passed is valid or not.

But since i'm sure my plist is well-formatted, i could use the method in question to see if the connection is available or not, instead of using the Reachability.h. It is of course just to understand if a data connection is available, not to understand what kind of connection is active(e.g. WiFi, etc). I say this because if i do a simple test like this in airplane mode, [dict count]; always returns 0.

NSURL * plist = [NSURL URLWithString: @ "http://www.example.com/example.plist"];
NSDictionary * dict = [[[NSDictionary alloc] initWithContentsOfURL:plist] autorelease];
  if ([dict count] == 0) {
      //no connection
  }

TIA.

Mat
  • 7,613
  • 4
  • 40
  • 56

1 Answers1

2

Yes you can do that. Keep in mind that [[NSDictionary alloc] initWithContentsOfURL:plist] is a synchronous blocking call. If you block the main thread too long then you'll get 0x8BADF00D, and the watch dog will kill your process.

logancautrell
  • 8,762
  • 3
  • 39
  • 50
  • you mean if the plist has a large size?, because i see no other reasons that could block the main thread for a long time. Correct me if i'm wrong. – Mat Nov 01 '11 at 16:24
  • If the network connect is slow for some reason, DNS resolution, any network problems that cause your request to be slooowww. – logancautrell Nov 01 '11 at 16:25
  • ..of course, and when you said "kill your process" you mean that the function returns nil or that the app crash?! – Mat Nov 01 '11 at 16:30
  • If you main thread blocks for too long (~10 seconds) then the system will kill your app, so yes it will crash. – logancautrell Nov 01 '11 at 16:39
  • now it's more clear. If no one offers a better answer, i consider it as accepted..thanks. – Mat Nov 01 '11 at 16:56