1

In my application the root view controller is calling multiple APIs to get data and then display it in a collection view.

Can I use background fetch here, to get API data in advance, so that when the user launches the app, the content is already available and stored in the database?

If not available for some reason (let’s say background fetch time has expired or hasn't triggered yet), then I can get them from the API anyway.

Chris
  • 4,009
  • 3
  • 21
  • 52
Matrix
  • 7,477
  • 14
  • 66
  • 97

1 Answers1

1

Yes this is a situation where background fetch would apply.

Up to and including iOS12, use the application(_:performFetchWithCompletionHandler:) method in your AppDelegate class. This code will run when iOS determines that it should, based on how the user uses the app.

You may need to get a reference to the relevant view controller from the app delegate and call the method you require.


For iOS13 and above, this method is replaced with BGAppRefreshTask.

These tasks must be registered when the app starts, using a BGTaskScheduler.


For both of these methods, you must enable the fetch background mode in your app’s entitlements.

Chris
  • 4,009
  • 3
  • 21
  • 52
  • @Matrix No problem. I’ve only used the first way myself, so can offer some examples if needed. Shouldn’t be too difficult to get your root view controller from the app delegate. I have not used the `BGAppRefreshTask` method yet myself. – Chris Dec 02 '19 at 14:57