0

On iOS. When an app gets sent to the background, Is the app still running on the same UI thread or does it get moved to a new thread?

StackMan
  • 55
  • 7

1 Answers1

1

The iOS allocated thread pool for your app is not altered, upon entering a background state (of course they are suspended though).

You can review which thread you are currently on by examining NSThread.Current.

Examples:

  • If you look at the thread in DidEnterBackground, yes it is the main thread.

  • A background fetch (PerformFetch) will be on the main thread.

  • Background-based Location updates are on the main thread.

i.e.

switch (UIApplication.SharedApplication.ApplicationState)
{
    case UIApplicationState.Background:
        Console.WriteLine($"Background location update {NSThread.Current}");
        break;
    case UIApplicationState.Active:
        Console.WriteLine($"Foreground location update {NSThread.Current}");
        break;
}

Location[16904:1947553] Foreground location update <NSThread: 0x60000006d900>{number = 1, name = main}
Location[16904:1947553] Foreground location update <NSThread: 0x60000006d900>{number = 1, name = main}
Location[16904:1947553] Foreground location update <NSThread: 0x60000006d900>{number = 1, name = main}
Location[16904:1947553] App entering background state.
Location[16904:1947553] Now receiving location updates in the background
Location[16904:1947553] Background location update <NSThread: 0x60000006d900>{number = 1, name = main}
Location[16904:1947553] Background location update <NSThread: 0x60000006d900>{number = 1, name = main}
SushiHangover
  • 73,120
  • 10
  • 106
  • 165
  • So my app sequences through a queue of downloads asynchronously on the UI thread. if the app is processing downloads and it goes into background, I know any downloads that have already been passed to the iOS download task will continue to download, but will downloads I still have queued on my UI thread still be processed and sent to the iOS download task? – StackMan Jan 30 '18 at 13:07
  • @StackMan Once you enter a background state (`DidEnterBackground`) you have about 20/30 seconds to wrap things up. You can request a background task via `BeginBackgroundTask` that will give you 5 to 10 minutes of app background processing before being suspended. Xamarin has a good guide that provides an overview of this: https://developer.xamarin.com/guides/ios/application_fundamentals/backgrounding/part_3_ios_backgrounding_techniques/ios_backgrounding_with_tasks/ – SushiHangover Jan 30 '18 at 13:21