Catch SQLiteConstraint exception in actfmsyncservice. Need to spend more time figuring out why it's happening

pull/14/head
Sam Bosley 12 years ago
parent 37bcac2575
commit 7096718764

@ -24,6 +24,7 @@ import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import android.content.ContentValues; import android.content.ContentValues;
import android.database.sqlite.SQLiteConstraintException;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.os.ConditionVariable; import android.os.ConditionVariable;
import android.text.TextUtils; import android.text.TextUtils;
@ -1151,12 +1152,17 @@ public final class ActFmSyncService {
if (remote.getValue(Task.USER_ID) != Task.USER_ID_SELF) if (remote.getValue(Task.USER_ID) != Task.USER_ID_SELF)
remote.putTransitory(SyncFlags.GTASKS_SUPPRESS_SYNC, true); remote.putTransitory(SyncFlags.GTASKS_SUPPRESS_SYNC, true);
// TODO: It seems like something about this title matching might be causing SQLiteConstraint exceptions. Think about it. // TODO: It seems like something about this title matching might be causing
// SQLiteConstraint exceptions. Think about it. In the meantime, catch and merge
if (!remote.isSaved() && gtasksPreferenceService.isLoggedIn()) { if (!remote.isSaved() && gtasksPreferenceService.isLoggedIn()) {
titleMatchOnGoogleTask(remote); titleMatchOnGoogleTask(remote);
} }
try {
taskService.save(remote); taskService.save(remote);
} catch (SQLiteConstraintException e) {
taskDao.handleSQLiteConstraintException(remote);
}
ids.add(remote.getId()); ids.add(remote.getId());
metadataService.synchronizeMetadata(remote.getId(), metadata, MetadataCriteria.withKey(TagService.KEY)); metadataService.synchronizeMetadata(remote.getId(), metadata, MetadataCriteria.withKey(TagService.KEY));
synchronizeAttachments(item, remote); synchronizeAttachments(item, remote);

@ -186,14 +186,7 @@ public class TaskDao extends DatabaseDao<Task> {
} catch (SQLiteConstraintException e) { } catch (SQLiteConstraintException e) {
if(e.getMessage().contains(Task.REMOTE_ID_PROPERTY_NAME)) { if(e.getMessage().contains(Task.REMOTE_ID_PROPERTY_NAME)) {
// Tried to create task with remote id that already exists // Tried to create task with remote id that already exists
saveSuccessful = false; saveSuccessful = handleSQLiteConstraintException(task);
TodorooCursor<Task> cursor = query(Query.select(Task.ID).where(
Task.REMOTE_ID.eq(task.getValue(Task.REMOTE_ID))));
if (cursor.getCount() > 0) {
cursor.moveToFirst();
task.setId(cursor.get(Task.ID));
saveSuccessful = saveExisting(task);
}
} }
} }
} else { } else {
@ -203,6 +196,17 @@ public class TaskDao extends DatabaseDao<Task> {
return saveSuccessful; return saveSuccessful;
} }
public boolean handleSQLiteConstraintException(Task task) {
TodorooCursor<Task> cursor = query(Query.select(Task.ID).where(
Task.REMOTE_ID.eq(task.getValue(Task.REMOTE_ID))));
if (cursor.getCount() > 0) {
cursor.moveToFirst();
task.setId(cursor.get(Task.ID));
return saveExisting(task);
}
return false;
}
@Override @Override
public boolean createNew(Task item) { public boolean createNew(Task item) {
if(!item.containsValue(Task.CREATION_DATE)) if(!item.containsValue(Task.CREATION_DATE))

Loading…
Cancel
Save