Reputation: 357
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. Here NSThread
function for running the system time. When the time is equal to 1:00 am I want to refresh the API. But it is not call the delegate of NSURLConnection
.
This is NSURLConnection code:
-(void)displays:(model *)place
{
NSString *strs=[@"http://www.earthtools.org/timezone-1.1/" stringByAppendingString:[NSString stringWithFormat:@"%@/%@",place.latitude,place.longitude]];
NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:strs]];
NSURLConnection *reqTimeZone=[NSURLConnection connectionWithRequest:request delegate:self];
[reqTimeZone start]; //here request not get start
}
Above code is with in function called "displays" argument is one instance of class it has all place details.
NSthread function code:
- (void) setTimer {
//assign current time
[self countDown];
}
- (void) countDown {
//count the current time
if(hrs==12&& meridian==@"pm")
[self display:(placedetails)];//it calls the displays function but NSURLConnection is not get start.
[NSThread detachNewThreadSelector:@selector(setTimer) toTarget:self withObject:nil];
}
Above display function is called placedetails assigned but NSURLConnection
delegate is not called.
Upvotes: 2
Views: 1365
Reputation: 6391
For the delegate methods to be called, you need to attach your thread's runloop to NSURLConnection. Since you are creating a thread and not attaching NSURLConnection to thread's RunLoop, connection delegate methods won't get fired.
Here is an example:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// I am creating a button and adding it to viewController's view
UIButton *bttn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[bttn setFrame:CGRectMake(100.0f, 200.0f, 120.0f, 50.0f)];
[bttn setTitle:@"Download" forState:UIControlStateNormal];
[bttn addTarget:self action:@selector(spawnThreadForDownload) forControlEvents:UIControlEventTouchUpInside];
[[self view] addSubview:bttn];
}
- (void)spawnThreadForDownload
{
[NSThread detachNewThreadSelector:@selector(downloadAndParse) toTarget:self withObject:nil];
}
- (void)downloadAndParse
{
@autoreleasepool {
NSURL *url = [NSURL URLWithString:@"http://apple.com"];
NSURLRequest *req = [NSURLRequest requestWithURL:url
cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData
timeoutInterval:20.0f];
NSURLConnection *conn = [NSURLConnection connectionWithRequest:req delegate:self];
// Run the currentRunLoop of your thread (Every thread comes with its own RunLoop)
[[NSRunLoop currentRunLoop] run];
// Schedule your connection to run on threads runLoop.
[conn scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
}
}
// NSURLConnectionDelegate methods
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"Connection failed with error: %@",[error localizedDescription]);
}
// NSURLConnectionDataDelegate methods
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"Connection finished downloading");
}
Upvotes: 4