Multi‐threading - Group22-MobileApp/Grupo22-Moviles GitHub Wiki
Multi-threading Strategies
Kotlin
For the Kotlin app, we implemented Multi-threading through the use of coroutines which allow us to manage Long-running tasks (this way we don't block the main thread) and provide Main-safety so that we can call any suspended function from the main thread.
We mainly started coroutines using launch and in the scope of the lifeCycleScope.
Flutter
References: APA: Napoli, Marco L. 2020. BEGINNING Flutter® A HANDS ON GUIDE TO APP DEVELOPMENT. ISBN: 978-1-119-55082-2
In Flutter, the framework is single-threaded, which means that multithreading is not directly available for concurrent processing. To prevent blocking the main thread and enable the execution of multiple tasks concurrently, Flutter utilizes asynchronous programming with Future functions. Futures represent a block of code that is intended to be executed in the future, allowing operations to run when the main thread is available without causing it to block. Asynchronous operations are extended from Futures to provide the flexibility of releasing the thread when handling high-priority tasks, ensuring efficient utilization of resources and responsiveness in the application.
For example:
The _firebaseService.createMaterialItem(newItem) method returns a Future that represents the asynchronous operation of creating a new item in Firebase database. Also, the then() method in Dart is used to handle the result of a Future once it completes or emits a value (Napoli, 2020). By adding the then() method to the Future, a callback function that will be executed when the Future completes successfully (Napoli, 2020). In the case of the code in the image, the then() method is used to show a success message using a SnackBar when the material item is added successfully to Firebase. It also clears the text controllers and resets the image state for a later use of the widget. On the other hand, the "async" placed before the function declaration indicates that the function is asynchronous. This allows the function to use await within its body to wait for asynchronous operations to complete without blocking the execution of the program (the main and unique main thread) (Napoli, 2020). In the example, the await keyword is used when calling the uploadImage function to upload images asynchronously, pausing the execution of the function until the uploadImage operation is completed, allowing the function to continue its execution after the result is available.
More on Futures:
In our code we use in 10 different files Future related functions or elements. All with the purpose of handling asynchronous operations efficiently. This allows our aplication to run smoothly and perform other tasks while waiting for asynchronous operations to complete.
Finally, the method FutureBuilder allows to define a future whose result will be used to build a widget tree (Napoli, 2020). Meaning that the widget tree will depend on the completion of the asynchronous operation of the future. Also, by declaring the widget with FutureBuilder, the future is constantly monitored by the widget that rebuilds everytime there is change.
In our case, this is used in the allItems view, where the user can visualize all the items that have been posted. The widget where the FutureBuilder is declared depends on the future function that brings all items from firebase. The FutureBuilder also handles connectivity issues and errors regarding the extraction of items from firebase.
FutureBuilder example: