Permalink
Browse files

Fix suspected starup bug in PollThread.

Certain implementations don't like AsyncTasks being instantiated on a non-UI thread.
  • Loading branch information...
1 parent 7f3c1e2 commit 56b71abd6706edf0cbd04ec57e7a5cb2210260ae @thomaslee committed Sep 4, 2012
Showing with 32 additions and 5 deletions.
  1. +32 −5 src/co/tomlee/frapp/PollThread.java
@@ -96,13 +96,30 @@ public void run() {
* Executes a MorePostsTask on the main UI thread.
*/
private final class TaskRunnable implements Runnable {
- private PostsSinceTask task = create();
+ /**
+ * It's important that we don't initialize this until the run() call.
+ */
+ private PostsSinceTask task;
+ private boolean cancelled;
/**
* Ask the task to cancel.
*/
public void cancel() {
- task.cancel(true);
+ synchronized (this) {
+ if (task != null) {
+ task.cancel(true);
+ }
+ cancelled = true;
+ }
+ }
+
+ private boolean isCancelled() {
+ final boolean cancelled;
+ synchronized (this) {
+ cancelled = this.cancelled;
+ }
+ return cancelled;
}
/*
@@ -111,16 +128,26 @@ public void cancel() {
*/
@Override
public void run() {
- task.cancel(true);
- task = create();
- task.execute();
+ //
+ // If we were cancelled by another thread, don't try to run anything.
+ //
+ if (!isCancelled()) {
+ cancel();
+ synchronized (this) {
+ task = create();
+ task.execute();
+ }
+ }
}
/**
* Create a new MorePostsTask with the appropriate parameters to
* pull posts we haven't seen yet.
*/
private PostsSinceTask create() {
+ synchronized (this) {
+ cancelled = false;
+ }
return new PostsSinceTask(stream, postsAdapter, client, postsAdapter.getNewestPostId());
}
}

0 comments on commit 56b71ab

Please sign in to comment.