Ashutosh
Ashutosh

Reputation: 5742

How to trigger an action after a certain amount of time?

In my app i want to display an error message if the network connection is too slow after few seconds. How should i implement this? Here's the code:

-(void)setProjectID:(NSString *)newProject {
    [self willChangeValueForKey:@"projectID"];
    [projectID release];
    projectID = [newProject copy];
    [self didChangeValueForKey:@"projectID"];

    // Since we have an ID, now we need to load it
    NSInvocation *returnInvocation = [NSInvocation invocationWithMethodSignature:
                                      [Detail instanceMethodSignatureForSelector:@selector(configureView:)]];
    [returnInvocation setTarget:self];
    [returnInvocation setSelector:@selector(configureView:)];
    [returnInvocation retainArguments];

    fetch = [[WBWDocumentFetcher alloc] init];
    [fetch retrieveDocument:[NSURL wb_URLForTabType:PROJECT_DETAILS inProject:projectID] returnBy:returnInvocation];
}
-(void)configureView:(NSDictionary *)serverResult 
{
}

Thanks,

Upvotes: 0

Views: 132

Answers (2)

Janak Nirmal
Janak Nirmal

Reputation: 22726

If you are using NSURLRequest for requesting server than use following code to judge time out.

//In this you can set timeoutinterval for request
NSURLRequest* request = [NSURLRequest requestWithURL:yourURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0]; 

//If request is failing for time out reason you can check that and alert user accordingly.
-(void)connectionFailed:(NSError *)error
{
UIAlertView *objAlert = [[UIAlertView alloc] init]; 
[objAlert setTitle:@"Internet Connection"];
[objAlert addButtonWithTitle:@"Ok"];
if([error code] == -1001 || [[error localizedDescription] isEqualToString:@"timed out"]){
        [objAlert setMessage:@"Request Timed Out."];
        [objAlert show];
    }
}

Upvotes: 0

PengOne
PengOne

Reputation: 48398

You want to use performSelector:afterDelay: or possibly performSelector:withObject:afterDelay:.

Then, at the beginning of the method called, check to see if the page has loaded. If not, then display a UIAlertView and cancel the load.

Upvotes: 1

Related Questions