-
Notifications
You must be signed in to change notification settings - Fork 828
fix: app freeze (ANR) when we check if a model has been downloaded (isModelDownloaded) #807
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
…-downloaded fix-anr-when-checking-if-model-downloaded
@helloimfrog : thanks for the PR, will review |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
Refactors model download checking from synchronous to asynchronous to fix Android ANR (Application Not Responding) issues. The change replaces blocking Tasks.await()
calls with proper callback-based async patterns using addOnSuccessListener
and addOnFailureListener
.
- Introduces a callback interface for asynchronous model download status checking
- Updates all ML Kit detector classes to use the new async model checking pattern
- Removes thread executor and blocking task operations that were causing ANR
Reviewed Changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.
File | Description |
---|---|
GenericModelManager.java | Core refactor to async callback pattern, removes blocking executor operations |
ObjectDetector.java | Updates remote object detection to use async model checking with inline processing |
ImageLabelDetector.java | Updates remote image labeling to use async model checking with inline processing |
DigitalInkRecognizer.java | Updates digital ink recognition to use async model checking pattern |
Comments suppressed due to low confidence (1)
packages/google_mlkit_digital_ink_recognition/android/src/main/java/com/google_mlkit_digital_ink_recognition/DigitalInkRecognizer.java:73
- The entire recognition logic is now nested inside the callback, making the code deeply indented and harder to read. Consider extracting the recognition logic into a separate private method to improve readability and maintainability.
genericModelManager.isModelDownloaded(
model,
new GenericModelManager.CheckModelIsDownloadedCallback() {
@Override
public void onModelDownloaded(Boolean isDownloaded) {
if (!isDownloaded) {
result.error("Model Error", "Model has not been downloaded yet ", null);
return;
}
String id = call.argument("id");
com.google.mlkit.vision.digitalink.DigitalInkRecognizer recognizer = instances.get(id);
if (recognizer == null) {
recognizer = DigitalInkRecognition.getClient(DigitalInkRecognizerOptions.builder(model).build());
instances.put(id, recognizer);
}
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
...google_mlkit_commons/android/src/main/java/com/google_mlkit_commons/GenericModelManager.java
Outdated
Show resolved
Hide resolved
...ion/android/src/main/java/com/google_mlkit_digital_ink_recognition/DigitalInkRecognizer.java
Outdated
Show resolved
Hide resolved
...image_labeling/android/src/main/java/com/google_mlkit_image_labeling/ImageLabelDetector.java
Outdated
Show resolved
Hide resolved
@helloimfrog : could you address the comments by Copilot |
…-downloaded update lints
@fbernaly Checked all comments by Copilot and I pushed a little update. Please review again |
I integrated google_mlkit_digital_ink_recognition into my app but it was freezed and caused ANR.
Tried with example app and have same result.
So I have a little changed on GenericModelManager, change the way to check if a model is downloaded by using Task callback (
addOnSuccessListener
andaddOnFailureListener
).Seems it works correctly and won't cause ANR anymore.