From 5b200ec84952bdffd3ee500797efb1cc4ec85417 Mon Sep 17 00:00:00 2001 From: Sam Bosley Date: Tue, 5 Feb 2013 18:57:35 -0800 Subject: [PATCH] Use a map from messages to callbacks instead of two parallel queues. This should resolve some timing bugs--callbacks will only be called in the same loop that their message ocurrs --- .../astrid/actfm/sync/ActFmSyncThread.java | 40 +++++++++---------- 1 file changed, 18 insertions(+), 22 deletions(-) diff --git a/astrid/plugin-src/com/todoroo/astrid/actfm/sync/ActFmSyncThread.java b/astrid/plugin-src/com/todoroo/astrid/actfm/sync/ActFmSyncThread.java index a36f26696..9d1bb9015 100644 --- a/astrid/plugin-src/com/todoroo/astrid/actfm/sync/ActFmSyncThread.java +++ b/astrid/plugin-src/com/todoroo/astrid/actfm/sync/ActFmSyncThread.java @@ -2,8 +2,10 @@ package com.todoroo.astrid.actfm.sync; import java.io.IOException; import java.util.Collections; +import java.util.HashMap; import java.util.LinkedList; import java.util.List; +import java.util.Map; import org.json.JSONArray; import org.json.JSONObject; @@ -48,7 +50,7 @@ public class ActFmSyncThread { private static final String ERROR_TAG = "actfm-sync-thread"; //$NON-NLS-1$ private final List> pendingMessages; - private final List pendingCallbacks; + private final Map, Runnable> pendingCallbacks; private final Object monitor; private Thread thread; @@ -102,10 +104,9 @@ public class ActFmSyncThread { synchronized(ActFmSyncThread.class) { if (instance == null) { List> syncQueue = Collections.synchronizedList(new LinkedList>()); - List callbackQueue = Collections.synchronizedList(new LinkedList()); ActFmSyncMonitor monitor = ActFmSyncMonitor.getInstance(); - instance = new ActFmSyncThread(syncQueue, callbackQueue, monitor); + instance = new ActFmSyncThread(syncQueue, monitor); taskDao.addListener(new SyncDatabaseListener(instance, ModelType.TYPE_TASK)); tagDataDao.addListener(new SyncDatabaseListener(instance, ModelType.TYPE_TAG)); @@ -118,10 +119,10 @@ public class ActFmSyncThread { return instance; } - private ActFmSyncThread(List> messageQueue, List callbackQueue, Object syncMonitor) { + private ActFmSyncThread(List> messageQueue, Object syncMonitor) { DependencyInjectionService.getInstance().inject(this); this.pendingMessages = messageQueue; - this.pendingCallbacks = callbackQueue; + this.pendingCallbacks = Collections.synchronizedMap(new HashMap, Runnable>()); this.monitor = syncMonitor; } @@ -142,7 +143,7 @@ public class ActFmSyncThread { if (!pendingMessages.contains(message)) { pendingMessages.add(message); if (callback != null) - pendingCallbacks.add(callback); + pendingCallbacks.put(message, callback); synchronized(monitor) { monitor.notifyAll(); } @@ -154,7 +155,6 @@ public class ActFmSyncThread { try { int batchSize = 1; List> messageBatch = new LinkedList>(); - List callbackBatch = new LinkedList(); while(true) { synchronized(monitor) { while ((pendingMessages.isEmpty() && !timeForBackgroundSync()) || !actFmPreferenceService.isLoggedIn()) { @@ -174,21 +174,11 @@ public class ActFmSyncThread { messageBatch.add(message); } - while (callbackBatch.size() < batchSize && !pendingCallbacks.isEmpty()) { - Runnable callback = pendingCallbacks.remove(0); - callbackBatch.add(callback); - } - + boolean refreshAfterBatch = false; if (messageBatch.isEmpty() && timeForBackgroundSync()) { messageBatch.add(BriefMe.instantiateBriefMeForClass(Task.class, NameMaps.PUSHED_AT_TASKS)); messageBatch.add(BriefMe.instantiateBriefMeForClass(TagData.class, NameMaps.PUSHED_AT_TAGS)); - callbackBatch.add(new Runnable() { - @Override - public void run() { - Intent refresh = new Intent(AstridApiConstants.BROADCAST_EVENT_REFRESH); - ContextManager.getContext().sendBroadcast(refresh); - } - }); + refreshAfterBatch = true; } if (!messageBatch.isEmpty() && checkForToken()) { @@ -231,12 +221,18 @@ public class ActFmSyncThread { Log.e(ERROR_TAG, "IOException", e); batchSize = Math.max(batchSize / 2, 1); } - for (Runnable r : callbackBatch) { - r.run(); + + for (ClientToServerMessage message : messageBatch) { + Runnable r = pendingCallbacks.remove(message); + if (r != null) + r.run(); + } + if (refreshAfterBatch) { + Intent refresh = new Intent(AstridApiConstants.BROADCAST_EVENT_REFRESH); + ContextManager.getContext().sendBroadcast(refresh); } messageBatch = new LinkedList>(); - callbackBatch = new LinkedList(); } } } catch (Exception e) {