NSThread

Discussion in 'iOS Programming' started by chhoda, May 10, 2010.

  1. chhoda macrumors 6502

    Joined:
    Oct 25, 2008
    #1
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    No. A thread is still part of your process which is suspended when you go into the background.

    See the NSThread documentation.


    Any scenario you would normally use a thread: typically when you have a long running task you wish to perform without freezing your apps UI.

    And before you ask: no I won't provide any sample code.
     
  3. chhoda thread starter macrumors 6502

    Joined:
    Oct 25, 2008
    #3
    Thanks I wont ask for code, but I used to do those long running tasks in a asynchronous timer. Was wondering what other benefit NSThread will provide than an asynchronous NStimer would not !
     
  4. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    Normally NSTimers are scheduled on the main NSRunloop. So you could potentially slow down or even block event processing resulting in the UI stuttering or even freezing completely...
     
  5. firewood macrumors 604

    Joined:
    Jul 29, 2003
    Location:
    Silicon Valley
    #5
    With an async timer, you need to chop up the long running routine yourself into short chunks (timewise), or you'll block the UI.

    With a background thread, you can have the routine run continuously (for hours, e.g. until the battery runs out), but the OS will chop it up for you to give (uniprocessor on the iPhone) CPU time to the main run loop and UI events.

    It's pretty much a matter of who controls the chopping up, and the priorities between the chopped up chunks of code.
     

Share This Page