开发者

How to implement "check for updates" concept in iphone app

I have pickerview which is showing times in minuts like (5,10,20). If user will select any of the time he will get updates after selected time. And my application will run as usual but after 5 minutes a开发者_如何学Python message will show some updates occured.

How I will implement this concept? What coding should I do?

As per my guess should I go with threading?


You could have a NSTimer which starts when you choose a time from your pickerview.

timer = [NSTimer scheduledTimerWithTimeInterval:pickerValueInSeconds target:self selector:@selector(updateMethod) userInfo:nil repeats:NO];

You choose no repeat, then in your updateMethod you make a request:

-(void)updateMethod
{
    NSString *url = @"url...";
    NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:someURL]
                                        cachePolicy:NSURLRequestUseProtocolCachePolicy
                                          timeoutInterval:20.0]; // with some timeout interval.

    // create the connection with the request
    // and start loading the data
    NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

    if(theConnection) 
    {
        // Create the NSMutableData to hold the received data.
        // receivedData is an instance variable declared elsewhere.
        systemsData = [[NSMutableData data] retain];
    } 
    else 
    {
        // Inform the user that the connection failed.
        NSLog(@"NSURLConnection failed!");
    }

}

Now in your

-(void)connectionDidFinishLoading:(NSURLConnection *)connection

and

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 

you take care of the data or error and then start a new timer..

Note that you also need to implement

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    [systemsData setLength:0];
}

and

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [systemsData appendData:data];
}


You can use a NSTimer.

You can read more about NSTimer here on stackoverflow or on the docs.


You can create a NSTimer and wait for the selected duration. Once timeout is completed

scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:

the message is fired to the selector. There are plenty of sample codes on google.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜