iphoneiosobjective-casiformdatarequest

How to continuously get Http response without crashing?


I am calling API continuously with 5 seconds interval using the following code:

NSURL *url=[NSURL URLWithString:@"any url"];

ASIFormDataRequest *request=[[ASIFormDataRequest alloc] initWithURL:url];

[request setRequestMethod:@"POST"];
[request setDelegate:self];
[request addRequestHeader:@"Accept" value:@"application/json"];
    [request addPostValue:app.userName forKey:@"username"];
[request addPostValue:app.token forKey:@"token"];
    [request setTimeOutSeconds:40];
[request startSynchronous];
    NSError *error = [request error];
if (!error)
{ //sucesss
    } "

The problem I have is after approximately 15-20 minutes, the app crashes with bad access error. I have enabled zombie objects but I didn't get any message on console. Am I doing anything wrong, or can you tell me a standard way to call an api continuously? Thanks.


Solution

  • Have you checked the app's memory usage? Is it possible that the way you are calling this every 5 seconds means that the objects you are alloc-initing aren't getting released, so you're just running out of memory? I'm assuming you're using NSTimer?

    (On a second note, why are you using a delegate for a synchronous call? You can sometimes get odd errors if you don't unset them before they are released.)

    In terms of standard ways to do this, the only thing I'd recommend is that you do this on a background thread then using notifications to update the UI.

    Also, check your calling code as if you call this every 5 seconds, but with a 40 second timeout you could be stacking things up a bit (unless you stop the timer at the start, then reset it to 5 seconds at the end of this code).