CodeMinion / another_brother

Another Brother Flutter SDK
BSD 3-Clause "New" or "Revised" License
21 stars 22 forks source link

App completely freezing during print #30

Open shubhjagani opened 2 years ago

shubhjagani commented 2 years ago

My app becomes completely frozen while the printer is printing. It's weird because everything seems to be wrapped properly with Future and async/await and it is only freezing when the _channel.invokeMethod("printImage", params); is being called.

CodeMinion commented 2 years ago

Hi @shubhjagani ! Thanks for reaching out about this one!

Could you also share your printing code to have a better idea? Also what are the devices specs where this is happening and the what's the size of your image you are trying to print?

Thanks in advance

shubhjagani commented 2 years ago

Hi @CodeMinion. Thanks for responding! I start by capturing the screenshot of one of my widgets screenshotController.capture().then((val) => PrintAPI.printLabel(context, val!));

the print API configures and connects to the QL-810W over wifi - I have specified the IP address

static Future<void> printLabel(BuildContext context, Uint8List labelData) async {
    brother.Printer? printer = await configurePrinter(context);
    if (printer != null) {
      final Completer<ui.Image> completer = Completer();
      ui.decodeImageFromList(labelData, (ui.Image img) {
        return completer.complete(img);
      });
      ui.Image image = await completer.future;
      await printer.printImage(image);
      return;
    }
  }

The app only freezes when the printer is actually printing the label, which is very weird to me. See the video below. Running on iPad 15.6.

I basically can't do anything until the print has completed and then it registers all the taps. https://youtu.be/H-IEJo9wVWA

I tried moving the print command to an isolate but kept running into errors because you can't use dart:ui on an Isolate. Also tried to use compute but no luck

CodeMinion commented 2 years ago

Thanks for the code and the video @shubhjagani. The panda looks super adorable!

The problem turned out to be on the native iOS side of another_brother. It was executing the print on the main thread thus causing the app to freeze until it was done. I have released version 0.0.28 which resolves the issue.

Thanks again for all the info, and don't hesitate to reach out if there is anything else I can help with.

shubhjagani commented 2 years ago

wow thanks @CodeMinion!! do you mind making the change on the ios-no-type-b branch as well

CodeMinion commented 2 years ago

Happy to help!

Not at all! I have pushed the changes to the ios-no-type-b branch.

Thanks again for the report!

shubhjagani commented 2 years ago

Hi @CodeMinion, thank you for this. Now the app doesn't freeze but the printer basically stops responding after the first print. Then when I close the app (or disconnect flutter) all the other things that I sent to the printer print out at the same time. There might be an issue with the new thread is getting stuck or not getting closed.

CodeMinion commented 2 years ago

Hey @shubhjagani ! That's a bummer. I am not able to reproduce that one on my side. The interesting thing is that the actual printing is done inside the official borther library here https://github.com/CodeMinion/another_brother/blob/main/ios/Classes/Method/PrintImageMethodCall.m#L73. So if closing the app causes the previously sent prints to come out at least we know it is getting that far.

Could you send me what steps you are performing that causes it to get in that state? Also, does restarting the printer in that case allow it to work as normal when you print or just get stuck in the same situation.

Thanks in advance!

shubhjagani commented 2 years ago

So my steps are

  1. send print command
  2. while printing send another
  3. while printing send another

At this point I'm actually seeing a crash on xcode with the error: Thread 1: EXC_BAD_ACCESS (code=1, address=0x84ca6e745e40)

In xcode debugger it says the error is on Thread 1: Thread 1 Queue : com.apple.main-thread (serial) and sometimes i see Error retrieving thread information: (os/kern) invalid argument (lldb)

shubhjagani commented 2 years ago

so restarting the printer did seem to fix the problem of it only printing after the app is killed but I believe there is some problem with thread management

CodeMinion commented 2 years ago

Thanks for the steps @shubhjagani ! Yes, I have the same feeling too. Let me take a look at it more and see what I find.

shubhjagani commented 2 years ago

@CodeMinion any updates here? still running into the same issues with crashing

CodeMinion commented 2 years ago

Hey @shubhjagani ! Unfortunately I might not be able to take a closer look this until this weekend.

In the meantime since it seems to be happening when sending multiple prints at once to the printer I would recommend setting up a queuing system on the app so each subsequent request is queued while one is being printed.