0

I am using ASIHTTPRequest to interact with my website. I am basically adding ASIHttpRequests to a queue. It all works fine if there is an internet connection. However, I guess the operation gets deleted if the request fails (ie, no internet connection)

Is there a way to queue and execute the request only when there's internet connection? If there's no internet connection, the request should be queued and automatically sent when the connection returns. If this is not possible, what is the best way to accomplish what I am trying here?

Thanks!

This is how I am setting up the queue right now :

NSURL *url = [NSURL URLWithString:@"http://www.tst.com.au/test.php?op=updateField"];

        ASIFormDataRequest *startHourRequest = [ASIFormDataRequest requestWithURL:url];

        NSDateFormatter *apiformatter = [[ [NSDateFormatter alloc] init] autorelease];
        [apiformatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
        NSString *webdate = [apiformatter stringFromDate:date];
        [startHourRequest setDelegate:appDelegate];
        [startHourRequest setPostValue:webdate forKey:@"fieldvalue"];
        [startHourRequest setPostValue:[appDelegate shiftuid] forKey:@"uid"];
        [startHourRequest setPostValue:editAttribute forKey:@"fieldname"];
        [startHourRequest setDidFinishSelector:@selector(requestDone:)];
        [startHourRequest setDidFailSelector:@selector(requestWentWrong:)];
        [[appDelegate queue] addOperation:startHourRequest];
Ayush Chaudhary
  • 716
  • 1
  • 8
  • 20

1 Answers1

1

Yes you can do that quite easily with the Reachability class:

First off you will check internet reachability before you send the initial request:

Reachability *reachability = [Reachability reachabilityForInternetConnection];    
NetworkStatus internetStatus = [reachability currentReachabilityStatus];
if (internetStatus != NotReachable) {
    //Do your request
}
else {
    //save your url to an array for use when the internet returns
}

and within the view controller or wherever you are sending the requests from you do this to listen for internet changes

- (void)viewDidLoad

    [[NSNotificationCenter defaultCenter]
     addObserver:self 
     selector:@selector(networkStatusChanged:) 
     name:kReachabilityChangedNotification 
     object:nil];

    internetReachable = [Reachability reachabilityForInternetConnection];
    [internetReachable startNotifier];
}

- (void)networkStatusChanged:(NSNotification *)notice {
    NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];
    if (internetStatus != NotReachable && [yourUrlsToSend count] > 0) {
        //send off your requests now that you have internet

    }
}

This all depends on including the reqchability files from apple which can be found here: enter link description here . Hope that helps

rooster117
  • 5,502
  • 1
  • 21
  • 19
  • Thanks for the answer. I also wanted to know if I can queue operations and also do a similar thing when the app is closed? – Ayush Chaudhary Jun 10 '12 at 15:12
  • What I basically mean is the request should be sent WHENEVER the internet connection comes back up. – Ayush Chaudhary Jun 10 '12 at 15:13
  • Well you would just add another listener for when the app is opened again. applicationDidBecomeActive is probably what you would listen for specifically and you would do the same check as when your internet status changes where if you had unsent urls you would send them at that time. – rooster117 Jun 10 '12 at 15:31
  • Will those unsent urls be retained even if the app got terminated? Also, do you know what happens to an operation in a queue when the app has terminated? – Ayush Chaudhary Jun 10 '12 at 15:35
  • Well I'm really speaking in general terms. If you want to have these urls persist from your app being hard closed then you will need to do quite a bit more work. The easiest thing to do might be to us a plist to store all unsent urls or NSUserDefaults to do something similar. In that case you would also need to listen for applicationWillResignActive and any unset urls will have to be stored at that point which will then be checked for and loaded into your array when the app starts back up again. I think at this point you need to look at the scope of your project. – rooster117 Jun 10 '12 at 15:40
  • If you aren't going to have a case where the user has more than 1 unsent urls very often then just save it in the defaults and check for it on start up. – rooster117 Jun 10 '12 at 15:41
  • Thanks! I think you are right about looking at the scope of my project. – Ayush Chaudhary Jun 10 '12 at 15:43
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/12356/discussion-between-ayush-chaudhary-and-rooster117) – Ayush Chaudhary Jun 10 '12 at 16:09