iOSDev
iOSDev

Reputation: 3617

How to periodically poll data from server on a background thread?

I have a chat like app, and to receive periodic chats, I want to poll server at regular intervals , say 5 mins..and check if there are any new messages.

As a beginner, I have tried simple NSURLConnection and send and receive data, but stuck while polling. What is a correct way of polling? Should I use NSTimer to poll regularly? Is there any sample code that can help me?

I have a timer running on main thread which invokes a background thread every 5 seconds and sends request to server. Now what happening is, the delegates for NSURLConnection are not getting called. What can be done?

Please help.

Thanks

Upvotes: 0

Views: 3131

Answers (3)

iOSDev
iOSDev

Reputation: 3617

Solved this with following method:

[self performSelectorOnMainThread:@selector(performPollinginBackground) withObject:nil   waitUntilDone:NO];

-(void)performPollinginBackground{


//call server task here


}

Upvotes: -1

Dhaval Panchal
Dhaval Panchal

Reputation: 2529

Continuing from answer of Patrick :- If you can try NSURLConnection without delegate,

    -(void)update:(id)objmessage
{
    NSString *post =[NSString stringWithFormat:@"timestamp=%@&user_id=%@",DatetimeString,UID];

    NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];

    NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
    [request setURL:[NSURL URLWithString:@"Your server URL"]];
    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/x-www-form-urlencoded charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:postData];

    NSError *error;
    NSURLResponse *response;
    NSData *returnData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

    returnString=[[NSString alloc]initWithData:returnData encoding:NSUTF8StringEncoding];
    if(!returnString)
    {
        UIAlertView *erroralert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"There is an error getting response" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
        [erroralert show];
        [erroralert release];
    }
    else 
    {
        NSLog(@"%@",returnString);
    }
    SBJSON *json = [[SBJSON new] autorelease];
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] initWithDictionary:[json objectWithString:returnString error:nil]];
    //NSLog(@"%@",dic);
}

Upvotes: 1

Patrick T Nelson
Patrick T Nelson

Reputation: 1254

You can launch an NSTimer on a background thread and make calls to the main thread periodically, this is how I do it:

[self performSelectorOnMainThread:@selector(LaunchTimer) withObject:nil waitUntilDone:NO];

Just make a "LaunchTimer" function that calls an update function at a certain interval, which makes a call to NSURLConnection, and when you receive the new messages, update the chat window by making a call to the main thread like so:

[self performSelectorOnMainThread:@selector(update:) withObject:newMessages waitUntilDone:NO];

Upvotes: 1

Related Questions