Thread Events & NSURLConnections...

Discussion in 'iOS Programming' started by DaveGee, Jul 19, 2008.

  1. DaveGee macrumors 6502a

    Jul 25, 2001
    Okay here's a question thats gonna be kinda messy for me to explain but lemme try my best....

    I have an app...

    The app does (among other things the following)

    1: Reads a few XML files

    2: Connects synchronously to a device on my network using a soap call and parse some more XML data

    3: Threads-out a thread that open an asynchronous connection to yet another soap service and just listens for XML status messages (this stays open until the app quits).

    4: Displays a list of items (obtained by one of the many XML data parses) on the iPhone/iPod screen... and change the items listed as XML status messages come in from the async connection from step 3....

    To my shock and amazement I got all this working and it doesn't crash -- not yet anyway! (lol).

    I just got approved for the 'official iPhone development program' and I paid my 99 bucks and can FINALLY debug my app directly on my iPod touch (instead of just the simulator)... I did all the stuff needed to get my app moved over to my device and my app STILL works!! Happy Happy Joy Joy!

    So on to my problem/question...

    When my iPod touch got into 'sleep mode' (screen blanks) as near as I can tell my asynchronous connection gets torn down... I'm sure this is a battery saving thing and it's okay... but how where do I add code to my app to reconnect the asynchronous connection and more to the point, when the asynchronous connection was killed did the thread get terminated too?

    I'm so new to cocoa I'm just not sure how/where to find the answers to my questions...

    If anyone has any insight/advice/input I'm all ears!

    Thanks in advance!

  2. rafta2009 macrumors newbie

    Jul 2, 2009
    Hi DaveGee

    The iApp I am developing is very similar to yours. But I have problem using NSThread (should do the connection to the XMLRPC in background)
    Would you be so kind and post your code including NSThread here?
  3. parapup macrumors 65816


    Oct 31, 2006
    The thread doing async IO will not be killed after resume. If the connection is disconnected, the thread will receive some specific error on the file descriptor you are using to connect to the remote server. So your thread should have logic to detect connection errors and attempt a reconnection to the server.

    Without seeing the code I am not sure how exactly you are doing the async IO - if you are using something like the select() system call which allows you to specify a timeout - you will get a timeout error after your thread resumes and you would need to select again - which will fail if the socket is disconnected and then you can initiate a reconnect - just an example.

    On a regular Mac you could subscribe to power events and tear down your connection / mark it dead before the computer goes to sleep - not sure if same/similar API exists on the phone side.

Share This Page