I have a NSTimer
which is making an HTTP Request every 25 seconds. This works fine, but when I go to another ViewController and then back. It Does not work probably. What am I doing wrong?
-(void)viewDidAppear:(BOOL)animated {
timer = [NSTimer timerWithTimeInterval:26.0 target:self selector:@selector(sendRequestToGetData:) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
}
NSTimer
-(void)sendRequestToGetData:(NSTimer *)timer
{
NSDictionary *headers = @{},
*parameters = @{};
UNIHTTPJsonResponse *response = [[UNIRest get:
^(UNISimpleRequest* request) {
[request setUrl:
@"http://localhost/getfixture.php/fixtures"
];
[request setHeaders:headers];
[request setParameters:parameters];
}] asJson];
NSData* rawBody = [response rawBody];
jsonResult = [NSJSONSerialization JSONObjectWithData: rawBody
options: NSJSONReadingMutableContainers
error: nil];
}
Try this :
NSTimer *myTimer;
-(void)viewDidAppear:(BOOL)animated {
myTimer = [NSTimer scheduledTimerWithTimeInterval:26.0
target:self
selector:@selector(sendRequestToGetData:)
userInfo:nil
repeats:YES];
}
- (void)viewDidDisappear:(BOOL)animated{
[myTimer invalidate];
myTimer=nil;
}