Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

[shared_preferences] Removed deprecated AsyncTask API #3481

Merged
merged 16 commits into from
Feb 23, 2021
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.1

* Removed deprecated [AsyncTask](https://developer.android.com/reference/android/os/AsyncTask) was deprecated in API level 30 ([#3481](https://github.com/flutter/plugins/pull/3481))

## 2.0.0

* Migrate to null-safety.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.1.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-all.zip
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

import android.content.Context;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Looper;
import android.util.Base64;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
Expand All @@ -21,6 +22,10 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
* Implementation of the {@link MethodChannel.MethodCallHandler} for the plugin. It is also
Expand Down Expand Up @@ -118,17 +123,24 @@ public void onMethodCall(MethodCall call, MethodChannel.Result result) {

private void commitAsync(
final SharedPreferences.Editor editor, final MethodChannel.Result result) {
new AsyncTask<Void, Void, Boolean>() {
@Override
protected Boolean doInBackground(Void... voids) {
return editor.commit();
}
final ExecutorService executor =
new ThreadPoolExecutor(0, 1, 30L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Following up from comments on another PR: what's the reason for re-creating this every time, rather than having one instance for the object that's re-used?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh yes - that should be moved to the constructor. Sorry for not catching this

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@blasten @SirusCodes Could one of you do a follow-up PR to adjust that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I would do that👍

final Handler handler = new Handler(Looper.getMainLooper());

@Override
protected void onPostExecute(Boolean value) {
result.success(value);
}
}.execute();
executor.execute(
new Runnable() {
@Override
public void run() {
final boolean response = editor.commit();
handler.post(
new Runnable() {
@Override
public void run() {
result.success(response);
}
});
}
});
}

private List<String> decodeList(String encodedList) throws IOException {
Expand Down
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.0
version: 2.0.1

flutter:
plugin:
Expand Down