Repository navigation
Closed
Labels
P2Important issues not at the top of the work listImportant issues not at the top of the work liste: embedderUsers of the Embedder APIUsers of the Embedder APIengineflutter/engine related. See also e: labels.flutter/engine related. See also e: labels.packageflutter/packages repository. See also p: labels.flutter/packages repository. See also p: labels.
Description
According to the wiki "...blocking the platform thread for inordinate amounts of time will not cause jank in Flutter applications,...".
Imagine a plugin with the following methods:
- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
if ([@"sleep" isEqualToString:call.method]) {
[NSThread sleepForTimeInterval:10.0f];
result(@"Good morning!");
} else {
result(FlutterMethodNotImplemented);
}
} @Override
public void onMethodCall(MethodCall call, Result result) {
if (call.method.equals("sleep")) {
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
result.success("Good morning!");
} else {
result.notImplemented();
}
}Calling these methods from Flutter causes the UI to freeze completely. As this happens both on Android and iOS this must be either by design or a bug. A full example project can be found here: https://github.com/cbenhagen/sleepy
[✓] Flutter (Channel dev, v0.9.1, on Mac OS X 10.14 18A389, locale de-CH)
• Flutter version 0.9.1 at /Users/ben/flutter
• Framework revision bf7c27095c (13 hours ago), 2018-09-18 13:39:21 -0700
• Engine revision 9e315e6465
• Dart version 2.1.0-dev.4.0.flutter-050561fd82
[✓] Android toolchain - develop for Android devices (Android SDK 27.0.3)
• Android SDK at /Users/ben/Library/Android/sdk
• Android NDK location not configured (optional; useful for native profiling support)
• Platform android-27, build-tools 27.0.3
• Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
• Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1024-b01)
• All Android licenses accepted.
[✓] iOS toolchain - develop for iOS devices (Xcode 10.0)
• Xcode at /Applications/Xcode.app/Contents/Developer
• Xcode 10.0, Build version 10A254a
• ios-deploy 2.0.0
• CocoaPods version 1.5.3
[✓] Android Studio (version 3.1)
• Android Studio at /Applications/Android Studio.app/Contents
• Flutter plugin version 28.0.1
• Dart plugin version 173.4700
• Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1024-b01)
[✓] Connected devices (2 available)
• Android SDK built for x86 • emulator-5554 • android-x86 • Android 9 (API 28) (emulator)
• iPhone XS • 48A5A62A-7DB6-4C21-A7A9-961A089519D1 • ios • iOS 12.0 (simulator)
• No issues found!
Reacted by Ben Hagen, Fulvio Satta, CDDelta, Avaaj Gyawali, mrawl, TxThinking, Vince Varga, Vishesh Handa, Marcin Stepnowski, Artur Stepniewski and 38 more
Reacted by tolotrasamuel and Alex Torres
Reacted by tolotrasamuel and Alex Torres
Reacted by tolotrasamuel
Metadata
Metadata
Assignees
Labels
P2Important issues not at the top of the work listImportant issues not at the top of the work liste: embedderUsers of the Embedder APIUsers of the Embedder APIengineflutter/engine related. See also e: labels.flutter/engine related. See also e: labels.packageflutter/packages repository. See also p: labels.flutter/packages repository. See also p: labels.
Activity
cbenhagen commentedon Jan 20, 2019
@zoechi can you reevaluate the importance of this please? Either I am being very stupid here or this is a quite serious issue. Hoping for the former of course.
YotaVGA commentedon Jan 21, 2019
I am experimenting exactly the same problem. I'd at least like to know if it is on purpose or not. If it is not the wiki should be a lot more clear than this, and I don't get why a future should be returned in this case...
This is my flutter doctor (only tested on android by now):
zoechi commentedon Jan 21, 2019
@cbenhagen the issue has not a single upvote so far https://github.com/flutter/flutter/wiki/Issue-hygiene#thumbs-up-reactions
cbenhagen commentedon Jan 21, 2019
@zoechi I also wish people would use the upvote feature more but while having many upvotes is a good sign that an issue is important to many users, having no upvotes does not have to mean it can not be important. People writing plugins will just work around this issue by using threads or a dispatch_queue instead upvoting an issue. Like here for example: #25959
@YotaVGA please also give this issue your upvote.
zoechi commentedon Jan 21, 2019
@cbenhagen The comment was meant as hint that not even you upvoted and for others who stumble upon the issue later to get the idea as well.
While upvotes are relevant they are not the only criteria, but there are currently 4000+ open issues and lots of them are important.
That being said, plugins and infrastructure are on the roadmap for the next quarter https://github.com/flutter/flutter/wiki/Roadmap#ecosystem.
mehmetf commentedon Feb 18, 2019
My understanding is that this is by design:
@chinmaygarde as a fact checker emeritus here :-)
chinmaygarde commentedon Feb 19, 2019
TL;DR: This is by design. The highlighted section from the wiki is part of a larger paragraph that provides more context and a specific recommendation against the example attached to this issue.
That sentence on the wiki is part of a larger discussion on platform threading behavior, the entirety of which I have pasted below:
Dissecting that paragraph,
Thread.sleep(10000);counters the recommendation made on that wiki page. If you wanted to achieve this same effect, you could setup a timer on the platform thread and signal the same after the timeout. Or, more realistically, perform the expensive operation on a background thread.Suggestions on making the section in the wiki clearer are welcome of course.
svaghar commentedon Mar 7, 2019
I have ran into exact same issue discussed here. The main design question here is: why the platform plugin code is run on the main ui thread? If there is a convincing answer, then this is not a bug. Otherwise the current design forces developer to create a background thread.
chinmaygarde commentedon Mar 7, 2019
The design was geared with the anticipation that plugins would be written to access platform specific APIs. The majority of such APIs on both iOS and Android are not thread safe and must be accessed from their "main" threads. The design didn't want to have authors of plugins immediately rethread all their platform plugin calls to the main thread.
For long running tasks, having the tasks run on a Flutter managed thread is also not very flexible. Would the engine have to maintain a thread or a pool or workers for parallel task execution? Besides , platforms already provide fairly ergonomic ways of dispatching tasks to worker threads (libDispatch on iOS for example).
Since the author of the plugin had the most knowledge about the kinds of tasks their plugin calls would make, this design was deemed most flexible and least surprising (to the majority of anticipated use cases).
svaghar commentedon Mar 7, 2019
@chinmaygarde thanks for explaining. couple of questions.
I read the The-Engine-architecture on flutter wiki which explains 4 threads. I see two threads in my logcat: One running flutter/dart code and one thread running the java plugin code. Plugin runs on the UI thread. Which thread is running the flutter code?
My "long" running platfrom code are camera2 operations and filters. These could take 100s msecs to 1 second. What is the best way to design this? On a separate worker thread (e.g. Looper)?
5 remaining items
p30arena commentedon Apr 11, 2019
I want to draw your attention to this thread:
#30911
You should not call Result methods from a thread other than the main thread.
derolf commentedon Aug 30, 2019
I am initializing some stuff when the PlatformView is created at startup of the app, which might take some time. During that time, the app freezes.
Is that intended behavior?
Fix for Android release builds hanging UI on StorageCipher initializa…
Fix for Android hanging UI on StorageCipher initialization.
Fix for issue #116: Android hanging UI on StorageCipher initialization (
markfili commentedon May 12, 2020
I opted for coroutines in
onMethodCall:this moves heavy operations to a background computation thread and then calls
resulton the main thread.added org.jetbrains.kotlinx:kotlinx-coroutines-core and kotlinx-coroutines-android to android folder's build.gradle
Ehesp commentedon Jun 2, 2020
This can be achieved on Android using the Task API fairly easily:
The
executorcan then be some thread pool (see Executors) - whatever you use depends on your performance & requirements.drriguz commentedon Jul 16, 2020
I have the same issue when implementing the encryptions plugin, I tried to use FlutterIsolate to wrap calls but it's super slower than before, so I ended using native threads.
In ios, a
DispatchQueueis used:And in Android is pretty much the same idea, just use a new thread and
Looper.getMainLooper()I want to know if there is some official guide about this?
darshankawar commentedon Oct 2, 2020
Closing this as working by design and per this and this comment.
If you think this was closed by mistake and should be kept open, please write in comments and I'll reopen.
Thanks.
github-actions commentedon Aug 12, 2021
This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of
flutter doctor -vand a minimal reproduction of the issue.