0

I am writing location based app, get weather information through API by using NSURLConnection for current/other places .At first time I sent request its working successfully. But next time I want to refer the information for same place it not working while NSURLConnection is not call the any delegate methods.

this is my code:

NSString *strs=[@"http://www.earthtools.org/timezone-1.1/" stringByAppendingString:[NSString stringWithFormat:@"%@/%@",place.latitude,place.longitude]];

NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:strs]];

self.reqTimeZone=[NSURLConnection connectionWithRequest:request delegate:self];
[self.reqTimeZone start]; 
0x8badf00d
  • 6,391
  • 3
  • 35
  • 68
Madhubalan K
  • 357
  • 5
  • 21
  • Does this block of code execute on main thread or background thread ? If you are running on background thread you need to attach connection to thread's runloop. – 0x8badf00d May 10 '12 at 13:25

1 Answers1

6

I assume you mean NSURLConnection (NSConnection doesn't exist). NSURLConnection can only be used once. See Reusing an instance of NSURLConnection.

Another gotcha with NSURLConnection is that it must be ran on a thread with a runloop. The main thread automatically has a run loop, but methods called on GCD and NSOperation threads need to have the runloop created explicitly. In practice you probably don't need to run NSURLConnection on a background thread. The download operation will not block the main thread. If you do decide to run NSURLConnection on a run loop the easiest way to do it is probably to create an NSOperation subclass and create the run loop inside of -main.

Community
  • 1
  • 1
Benedict Cohen
  • 11,912
  • 7
  • 55
  • 67
  • i sent NSURLConnection request it is working fine.now i want to refresh the information i.e resend the NSURLConnection.Refresh is working when call from IBAction of button.But is not working from NSThread method. how to i solve this problem.Thank for replay – Madhubalan K May 10 '12 at 13:35