Search code examples
iosxamarinxamarin.ioscoreml

Xamarin.iOS coreML Get Prediction null reference error in background mode


model.GetPrediction method returns a null object when the iOS app is running in background mode. The method return correct predictions when app is in foreground. Has anyone else come across this issue?


Solution

  • Couple of things:

    You have very limited time to execute arbitrary code when the app is Backgrounded.

    • You can check BackgroundTimeRemaining to determine how much time you have left.
    • You have to perform your predictions within a BeginBackgroundTask action

    The foregrounded app (including Springboard) has priority over the GPU.

    • The foregrounded app GPU processing will not be interrupted to perform your background task-based prediction.

    • You can request predictions to use the CPU (UsesCpuOnly) by supplying the MLPredictionOptions to your prediction call.

    Your model should be restricted to the CPU if it might run in the background or if your app has other GPU intensive tasks.

    re: https://developer.apple.com/documentation/coreml/mlpredictionoptions/2921288-usescpuonly?language=objc