mik3y / usb-serial-for-android

Android USB host serial driver library for CDC, FTDI, Arduino and other devices.
MIT License
4.81k stars 1.58k forks source link

onNewData is stopping current run #530

Closed recser closed 9 months ago

recser commented 1 year ago

Hello, a question regarding any recommendations for implementing the onNewData functionality. Currently due to some processing inside onNewData method (splitting, deduping and so on) the execution of the method takes some small amount the time during which other new events of new data may come which interrupts current run and starts evaluating new data. Is there a proven method to handle this kind of scenarios ? I am currently implementing a queue based approach where onNewData would push new data into the queue, and another thread would read from that queue and process separately the data. Was wondering if this is the right approach or if there are any other ways of achieving a completion of processing before new data comes in?

kai-morich commented 1 year ago

if your data should be shown on the UI you could process it asynchronously by the UI mainLooper, e.g. as done here and here. If appended to a TextView the UI processing gets slow when already a lot of text is shown, so the second linked variant also takes care of merging received data.

recser commented 9 months ago

thank you for taking your time to answer, appreciated!