googlesamples / googlesamples/mlkit

[Bug report] iOS swift: 'Background Task was created 30 seconds ago' warning when downloading model

Open
#774 0 comments 0 reactions 1 assignee Claimed by @lipenny00 View on GitHub
Dominant language
Kotlin
Stars
4.3k
Forks
3.1k
PR merge metrics
No merged PRs in 30d

Description

**Describe the bug**
When downloading a translator language model with a slow internet connection the following warning is produced:
``` "Background Task # ("GTMSessionFetcher-redirector.gvt1.com"), was created over 30 seconds ago. In applications running in the background, this creates a risk of termination. Remember to call UIApplication.endBackgroundTask(_:) for your task in a timely manner to avoid this." ```

**To Reproduce**
using iOS 17 as the target OS for your project, open Xcode, and use Swift UI, make a Global class that will be passed to EnvironmentObject. Inside the Global class, make a class called Translator and put the function below in it; access the environmentObject in swiftUI and put it in a button. When you click look at the Network utility tool to see what is downloading.

If applicable, please include `code snippet` and `sample input(image, video, text, etc)`
```swift
func download(){
// Download the French model.
let frenchModel = TranslateRemoteModel.translateRemoteModel(language: .french)

// Keep a reference to the download progress so you can check that the model
// is available before you use it.
progress = ModelManager.modelManager().download(
frenchModel,
conditions: ModelDownloadConditions(
allowsCellularAccess: false,
allowsBackgroundDownloading: false
)
)
NotificationCenter.default.addObserver(
forName: .mlkitModelDownloadDidSucceed,
object: nil,
queue: nil
) { [weak self] notification in
guard let strongSelf = self,
let userInfo = notification.userInfo,
let model = userInfo[ModelDownloadUserInfoKey.remoteModel.rawValue]
as? TranslateRemoteModel,
model == frenchModel
else { return }
// The model was downloaded and is available on the device
//success callback here
}

NotificationCenter.default.addObserver(
forName: .mlkitModelDownloadDidFail,
object: nil,
queue: nil
) { [weak self] notification in
guard let strongSelf = self,
let userInfo = notification.userInfo,
let model = userInfo[ModelDownloadUserInfoKey.remoteModel.rawValue]
as? TranslateRemoteModel
else { return }
let error = userInfo[ModelDownloadUserInfoKey.error.rawValue]
// error callback here
}
}

```
**Expected behavior**
Not to get the warning message

**SDK Info:**
- MLKit Translator 'GoogleMLKit/Translate', '3.2.0'

**Smartphone:**
- iPhone 11 - iOS 17.2

**Development Environment:**
*(For Android issue feel free to skip this section)*
- IDE Eversion: xcode 15.2
- Laptop/Desktop: macpro 2020 m1
- Laptop/Desktop OS/version: Sonoma 14.3.1

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.