Skip to content

fix: app freeze (ANR) when we check if a model has been downloaded (isModelDownloaded)#807

Merged
fbernaly merged 4 commits into
flutter-ml:developfrom
helloimfrog:develop
Sep 17, 2025
Merged

fix: app freeze (ANR) when we check if a model has been downloaded (isModelDownloaded)#807
fbernaly merged 4 commits into
flutter-ml:developfrom
helloimfrog:develop

Conversation

@helloimfrog

Copy link
Copy Markdown
Contributor

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 and addOnFailureListener).
Seems it works correctly and won't cause ANR anymore.

Nguyen Ngoc Long and others added 2 commits September 11, 2025 11:06
@fbernaly

Copy link
Copy Markdown
Collaborator

@helloimfrog : thanks for the PR, will review

Copilot AI left a comment

Copy link
Copy Markdown

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.

@fbernaly

Copy link
Copy Markdown
Collaborator

@helloimfrog : could you address the comments by Copilot

Nguyen Ngoc Long and others added 2 commits September 12, 2025 10:33
@helloimfrog

Copy link
Copy Markdown
Contributor Author

@fbernaly Checked all comments by Copilot and I pushed a little update. Please review again

@fbernaly fbernaly merged commit 3654338 into flutter-ml:develop Sep 17, 2025
2 checks passed
MatthiasKi added a commit to MatthiasKi/google_ml_kit_flutter that referenced this pull request Oct 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants