Some ideas for fixing timing issue in astrid.com

pull/14/head
Sam Bosley 13 years ago
parent c44b884f3a
commit cfdbe08e75

@ -12,6 +12,8 @@ import java.util.HashSet;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Queue; import java.util.Queue;
import java.util.concurrent.Semaphore;
import java.util.concurrent.atomic.AtomicInteger;
import org.apache.http.entity.mime.MultipartEntity; import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.ByteArrayBody; import org.apache.http.entity.mime.content.ByteArrayBody;
@ -112,8 +114,10 @@ public final class ActFmSyncService {
} }
private final List<FailedPush> failedPushes = Collections.synchronizedList(new LinkedList<FailedPush>()); private final List<FailedPush> failedPushes = Collections.synchronizedList(new LinkedList<FailedPush>());
Thread pushRetryThread = null; private Thread pushRetryThread = null;
Runnable pushRetryRunnable; private Runnable pushRetryRunnable;
private final AtomicInteger taskPushThreads = new AtomicInteger(0);
private Semaphore waiter = null;
public void initialize() { public void initialize() {
initializeRetryRunnable(); initializeRetryRunnable();
@ -135,9 +139,17 @@ public final class ActFmSyncService {
new Thread(new Runnable() { new Thread(new Runnable() {
@Override @Override
public void run() { public void run() {
taskPushThreads.incrementAndGet();
// sleep so metadata associated with task is saved // sleep so metadata associated with task is saved
AndroidUtilities.sleepDeep(1000L); AndroidUtilities.sleepDeep(1000L);
pushTaskOnSave(model, setValues); pushTaskOnSave(model, setValues);
synchronized(ActFmSyncService.this) {
if (taskPushThreads.decrementAndGet() == 0) {
if (waiter != null) {
waiter.release();
}
}
}
} }
}).start(); }).start();
} }
@ -235,6 +247,23 @@ public final class ActFmSyncService {
} }
} }
public void waitUntilEmpty() {
synchronized(this) {
if (taskPushThreads.get() > 0)
waiter = new Semaphore(0);
else
return;
}
try {
waiter.acquire();
} catch (InterruptedException e) {
// Ignored
}
synchronized(this) {
waiter = null;
}
}
// --- data push methods // --- data push methods
/** /**

@ -118,6 +118,7 @@ public class ActFmSyncV2Provider extends SyncV2Provider {
startTagFetcher(callback, finisher); startTagFetcher(callback, finisher);
actFmSyncService.waitUntilEmpty();
startTaskFetcher(manual, callback, finisher); startTaskFetcher(manual, callback, finisher);
callback.incrementProgress(50); callback.incrementProgress(50);
@ -255,6 +256,7 @@ public class ActFmSyncV2Provider extends SyncV2Provider {
fetchTagData(tagData, noRemoteId, manual, callback, finisher); fetchTagData(tagData, noRemoteId, manual, callback, finisher);
if(!noRemoteId) { if(!noRemoteId) {
actFmSyncService.waitUntilEmpty();
fetchTasksForTag(tagData, manual, callback, finisher); fetchTasksForTag(tagData, manual, callback, finisher);
fetchUpdatesForTag(tagData, manual, callback, finisher); fetchUpdatesForTag(tagData, manual, callback, finisher);
} }

Loading…
Cancel
Save