8000 [shared_preferences] Don't create additional Handler when method channel is called. by hoc081098 · Pull Request #3639 · flutter/plugins · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

[shared_preferences] Don't create additional Handler when method channel is called. #3639

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/shared_preferences/shared_preferences/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.0.3

* Android: don't create additional Handler when method channel is called.

## 2.0.2

* Don't create additional thread pools when method channel is called.
Expand Down
10000
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class MethodCallHandlerImpl implements MethodChannel.MethodCallHandler {
private final android.content.SharedPreferences preferences;

private final ExecutorService executor;
private final Handler handler;

/**
* Constructs a {@link MethodCallHandlerImpl} instance. Creates a {@link
Expand All @@ -53,6 +54,7 @@ class MethodCallHandlerImpl implements MethodChannel.MethodCallHandler {
preferences = context.getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
executor =
new ThreadPoolExecutor(0, 1, 30L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>());
handler = new Handler(Looper.getMainLooper());
}

@Override
Expand Down Expand Up @@ -125,10 +127,13 @@ public void onMethodCall(MethodCall call, MethodChannel.Result result) {
}
}

public void teardown() {
handler.removeCallbacksAndMessages(null);
executor.shutdown();
}

private void commitAsync(
final SharedPreferences.Editor editor, final MethodChannel.Result result) {
final Handler handler = new Handler(Looper.getMainLooper());

executor.execute(
new Runnable() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
public class SharedPreferencesPlugin implements FlutterPlugin {
private static final String CHANNEL_NAME = "plugins.flutter.io/shared_preferences";
private MethodChannel channel;
private MethodCallHandlerImpl handler;

@SuppressWarnings("deprecation")
public static void registerWith(io.flutter.plugin.common.PluginRegistry.Registrar registrar) {
Expand All @@ -32,11 +33,13 @@ public void onDetachedFromEngine(FlutterPlugin.FlutterPluginBinding binding) {

private void setupChannel(BinaryMessenger messenger, Context context) {
channel = new MethodChannel(messenger, CHANNEL_NAME);
MethodCallHandlerImpl handler = new MethodCallHandlerImpl(context);
handler = new MethodCallHandlerImpl(context);
channel.setMethodCallHandler(handler);
}

private void teardownChannel() {
handler.teardown();
handler = null;
channel.setMethodCallHandler(null);
channel = null;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/shared_preferences/shared_preferences/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: shared_preferences
description: Flutter plugin for reading and writing simple key-value pairs.
Wraps NSUserDefaults on iOS and SharedPreferences on Android.
homepage: https://github.com/flutter/plugins/tree/master/packages/shared_preferences/shared_preferences
version: 2.0.2
version: 2.0.3

flutter:
plugin:
Expand Down
0