I have a timer that I use to check a web address every 11 seconds. I use ASIHTTPRequest to set a delegate which reports the response form the web server. If I get the response I’m looking for, I want to update the UI. Unfortunately this doesn’t work, I suspect because I am not updating the UI from the UI thread.
Code to start and process timer:
- (void)startTimer {
BOOL timerStarted = false;
BOOL running = false;
BOOL readyToProcess = false;
if(!timerStarted){
running = true;
timerStarted = true;
readyToProcess = true;
autosaveTimer = [[NSTimer scheduledTimerWithTimeInterval:11
target:self
selector:@selector(processWebCheckTimer:)
userInfo:nil
repeats:YES] retain];
}
}
- (void)processWebCheckTimer:(NSTimer*)theTimer {
if ([VariableStore sharedInstance].success < 1) { //conversion not done, check again
NSURL *url = [NSURL URLWithString:@"WEBSERVERURLHERE"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:oc_request forKey:@"queue"];
[request setRequestMethod:@"POST"];
[request setDelegate:self];
[request startAsynchronous];
}
}
Check response and update UI:
- (void)requestFinished:(ASIFormDataRequest *)request
{
// Use when fetching text data
NSString *responseString = [request responseString];
if ([responseString rangeOfString:@"uccessfully converted"].location != NSNotFound) {
[self performSelectorOnMainThread:@selector(threadStopAnimatingTwo:) withObject:nil waitUntilDone:YES]; //DOESNT WORK
[autosaveTimer invalidate];
}
}
Function to Update UI:
- (void) threadStopAnimatingTwo:(id)data {
NSLog(@"stop animating two");
[loadingTwo stopAnimating];
[labelTwo setTextColor:[UIColor lightGrayColor]];
}
It turns out that I was creating a new instance of my View controller when I didn’t need to. Because I created a new instance of the view controller,
[self.loadingTwo]referred to anullUI component.