Skip to content

Commit

Permalink
Protecting against concurrent modification of JSONObject
Browse files Browse the repository at this point in the history
Fixes #158
  • Loading branch information
Robert Collins committed Nov 18, 2014
1 parent f656ecc commit 0551c21
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
Expand Up @@ -82,12 +82,23 @@ public void prepare(Bucket<T> bucket) {
*/
@Override
public void save(T object, List<Index> indexes) {

// Concurrencey protection synchronizing on diffalbe value
// real fix will be using a copy of an Syncable's data instead
// of accessing the syncable. See #158 and #159
JSONObject data = object.getDiffableValue();

String raw = "";
synchronized (data) {
raw = data.toString();
}

String key = object.getSimperiumKey();
mReindexer.skip(key);
ContentValues values = new ContentValues();
values.put("bucket", mBucketName);
values.put("key", key);
values.put("data", object.getDiffableValue().toString());
values.put("data", raw);
Cursor cursor = queryObject(mBucketName, key);
if (cursor.getCount() == 0) {
mDatabase.insert(OBJECTS_TABLE, null, values);
Expand Down
8 changes: 7 additions & 1 deletion Simperium/src/main/java/com/simperium/client/Syncable.java
Expand Up @@ -36,7 +36,13 @@ public Boolean isNew() {
* Does the local object have modifications?
*/
public Boolean isModified() {
return !JSONDiff.equals(getDiffableValue(), mGhost.getDiffableValue());

// Protected against Concurrent modification exception: see #158
JSONObject value = getDiffableValue();

synchronized (value) {
return !JSONDiff.equals(value, mGhost.getDiffableValue());
}
}

public String getBucketName() {
Expand Down

0 comments on commit 0551c21

Please sign in to comment.