I’m very much new to objective C. I have a case where I need to send request to the server URL at regular intervals. I made a asynchronous URL connection and used a NSTimer function to call the viewWillAppear function.
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"myurl/test.csv"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:15.0];
NSURLConnection *connection= [[NSURLConnection alloc] initWithRequest:request delegate:self];
if(connection){
label.text = @"connecting...";
}else{
//
}
}
-(void)connection :(NSURLConnection *) connection didReceiveData:(NSData *)data{
[self viewWillAppear:TRUE];
response = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(response);
}
I used the following NSTimer method to call the viewWillAppear method.
- (void)checkURLRequest
{
[self setProgressTimer:[NSTimer
scheduledTimerWithTimeInterval:(1.0f / 30.0f)
target:self
selector:@selector(viewWillAppear:)
userInfo:self
repeats:TRUE]];
}
- (void)setProgressTimer:(NSTimer *)theTimer
{
[_progressTimer invalidate];
_progressTimer = theTimer;
}
Is my approach even correct? Because when i updated the file ‘test.csv’ on the server, it can’t do a live update as and when the file changes its still logging the old values in the file.
I would move all the URL connection stuff outside the VC and into a custom class that does this using blocks. This would allow you to use this in other classes and is better OO programming. I can send you an example I use with image loading if you need it.