How to make 2 consecutive URL requests

Discussion in 'iOS Development' started by cybohemia, Feb 14, 2009.

  1. cybohemia

    cybohemia New Member

    Joined:
    Jan 25, 2009
    Messages:
    49
    Likes Received:
    0
    Device:
    iPhone 3G (Black)
    I was able to use NSURLConnection to make a URL request and, in didReceiveData, do stuff with the returned data.

    What I need to do is to make a connection immediately beforehand if a particular condition exists.

    So, in my routine, I have something like:

    Code:
    if (x) [self make_url_request1];
    [self make_url_request2];
    
    But I'm a bit confused about the asynchronous nature of the requests: request2 may get served faster than request1 - how would I distinguish that in the didReceiveData code?

    I've tried putting in flags to check but that seems pretty brutish and I'm not sure it's fail-safe.

    (Actually, I may want request1 to complete before doing request2 so I may end up changing the logic entirely but I'm still interested in how the above case should be done.)

    Any ideas and suggestions are greatly appreciated!
  2. JoshuaCaputo

    JoshuaCaputo New Member

    Joined:
    Aug 2, 2008
    Messages:
    605
    Likes Received:
    0
    Device:
    iPod touch
    maybe a timer or callback?
  3. cybohemia

    cybohemia New Member

    Joined:
    Jan 25, 2009
    Messages:
    49
    Likes Received:
    0
    Device:
    iPhone 3G (Black)
    Hi, Joshua. Thanks for the thoughts.

    Timers seem kind of risky given the asynchronous nature - unless I'm mis-understanding.

    I'm using the system callbacks but they seem to be generic - I don't know how to distinguish the callback for the first request from the callback for the second request.
  4. JoshuaCaputo

    JoshuaCaputo New Member

    Joined:
    Aug 2, 2008
    Messages:
    605
    Likes Received:
    0
    Device:
    iPod touch
    I don't know at all.
    Timers are very risky.
  5. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    [self performSelectorOnMainThread:selector waitUntilDone:YES];
  6. JoshuaCaputo

    JoshuaCaputo New Member

    Joined:
    Aug 2, 2008
    Messages:
    605
    Likes Received:
    0
    Device:
    iPod touch
    dun dun da da!!!! GOD has arrived with the answer

    Please Register or Log in to view images

  7. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    It's not the way I'd do this in my own app, but it is the simplest way to do this.

Share This Page