Skip to content

Commit a0b6ece

Browse files
authored
Merge pull request #20943 from wordpress-mobile/hackweek/database-anr
Fix a potential issue related to lifecycle
2 parents 689aecc + e5866cb commit a0b6ece

4 files changed

Lines changed: 63 additions & 37 deletions

File tree

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package org.wordpress.android.datasets
2+
3+
import kotlinx.coroutines.CoroutineDispatcher
4+
import kotlinx.coroutines.CoroutineScope
5+
import kotlinx.coroutines.Dispatchers
6+
import kotlinx.coroutines.launch
7+
import kotlinx.coroutines.withContext
8+
9+
/**
10+
* Helper class to handle asynchronous I/O tasks using coroutines
11+
* @see <a href="https://github.com/wordpress-mobile/WordPress-Android/pull/20937">Introduction</a>
12+
*/
13+
object AsyncTaskExecutor {
14+
/**
15+
* Execute a data loading task in the IO thread and handle the result on the main thread
16+
*/
17+
@JvmStatic
18+
fun <T> executeIo(scope: CoroutineScope, backgroundTask: () -> T, callback: AsyncTaskCallback<T>) {
19+
execute(scope, Dispatchers.IO, backgroundTask, callback)
20+
}
21+
22+
/**
23+
* Execute a data loading task in the default thread and handle the result on the main thread
24+
*/
25+
@JvmStatic
26+
fun <T> executeDefault(scope: CoroutineScope, backgroundTask: () -> T, callback: AsyncTaskCallback<T>) {
27+
execute(scope, Dispatchers.Default, backgroundTask, callback)
28+
}
29+
30+
private fun <T> execute(
31+
scope: CoroutineScope,
32+
dispatcher: CoroutineDispatcher,
33+
backgroundTask: () -> T,
34+
callback: AsyncTaskCallback<T>
35+
) {
36+
scope.launch(dispatcher) {
37+
// handle the background task
38+
val result = backgroundTask()
39+
40+
withContext(Dispatchers.Main) {
41+
// handle the result on the main thread
42+
callback.onTaskFinished(result)
43+
}
44+
}
45+
}
46+
47+
interface AsyncTaskCallback<T> {
48+
fun onTaskFinished(result: T)
49+
}
50+
}
51+

WordPress/src/main/java/org/wordpress/android/datasets/AsyncTaskHandler.kt

Lines changed: 0 additions & 33 deletions
This file was deleted.

WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderPostListFragment.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@
153153

154154
import javax.inject.Inject;
155155

156+
import static androidx.lifecycle.LifecycleOwnerKt.getLifecycleScope;
156157
import static org.wordpress.android.fluxc.generated.AccountActionBuilder.newUpdateSubscriptionNotificationPostAction;
157158
import static org.wordpress.android.ui.reader.ReaderActivityLauncher.OpenUrlType.INTERNAL;
158159

@@ -1923,7 +1924,8 @@ private ReaderPostAdapter getPostAdapter() {
19231924
mImageManager,
19241925
mUiHelpers,
19251926
mNetworkUtilsWrapper,
1926-
mIsTopLevel
1927+
mIsTopLevel,
1928+
getLifecycleScope(this)
19271929
);
19281930
mPostAdapter.setOnFollowListener(this);
19291931
mPostAdapter.setOnPostSelectedListener(this);

WordPress/src/main/java/org/wordpress/android/ui/reader/adapters/ReaderPostAdapter.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@
1515
import android.widget.TextView;
1616

1717
import androidx.annotation.NonNull;
18+
import androidx.lifecycle.LifecycleCoroutineScope;
1819
import androidx.recyclerview.widget.RecyclerView;
1920

2021
import org.wordpress.android.R;
2122
import org.wordpress.android.WordPress;
2223
import org.wordpress.android.analytics.AnalyticsTracker;
23-
import org.wordpress.android.datasets.AsyncTaskHandler;
24+
import org.wordpress.android.datasets.AsyncTaskExecutor;
2425
import org.wordpress.android.datasets.ReaderPostTable;
2526
import org.wordpress.android.datasets.ReaderTagTable;
2627
import org.wordpress.android.fluxc.store.AccountStore;
@@ -82,11 +83,13 @@
8283
import kotlin.jvm.functions.Function1;
8384
import kotlin.jvm.functions.Function2;
8485
import kotlin.jvm.functions.Function3;
86+
import kotlinx.coroutines.CoroutineScope;
8587

8688
public class ReaderPostAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
8789
private final ImageManager mImageManager;
8890
private final UiHelpers mUiHelpers;
8991
private final NetworkUtilsWrapper mNetworkUtilsWrapper;
92+
private final CoroutineScope mScope;
9093
private ReaderTag mCurrentTag;
9194
private long mCurrentBlogId;
9295
private long mCurrentFeedId;
@@ -372,7 +375,8 @@ private void toggleFollowButton(
372375
return;
373376
}
374377

375-
AsyncTaskHandler.load(
378+
AsyncTaskExecutor.executeIo(
379+
mScope,
376380
() -> !ReaderTagTable.isFollowedTagName(currentTag.getTagSlug()),
377381
isAskingToFollow -> {
378382
final String slugForTracking = currentTag.getTagSlug();
@@ -688,7 +692,8 @@ public ReaderPostAdapter(
688692
ImageManager imageManager,
689693
UiHelpers uiHelpers,
690694
@NonNull final NetworkUtilsWrapper networkUtilsWrapper,
691-
boolean isMainReader
695+
boolean isMainReader,
696+
LifecycleCoroutineScope scope
692697
) {
693698
super();
694699
((WordPress) context.getApplicationContext()).component().inject(this);
@@ -699,6 +704,7 @@ public ReaderPostAdapter(
699704
mNetworkUtilsWrapper = networkUtilsWrapper;
700705
mAvatarSzSmall = context.getResources().getDimensionPixelSize(R.dimen.avatar_sz_small);
701706
mIsMainReader = isMainReader;
707+
mScope = scope;
702708

703709
int displayWidth = DisplayUtils.getWindowPixelWidth(context);
704710
int cardMargin = context.getResources().getDimensionPixelSize(R.dimen.reader_card_margin);

0 commit comments

Comments
 (0)