Skip to content

Attempt to re-save installation if Installation was deleted on the server #579

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions Parse/src/main/java/com/parse/ParseInstallation.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,28 @@ public Task<T> then(Task<Void> task) throws Exception {
}
}

@Override
/* package */ Task<Void> saveAsync(final String sessionToken, final Task<Void> toAwait) {
return super.saveAsync(sessionToken, toAwait).continueWithTask(new Continuation<Void, Task<Void>>() {
@Override
public Task<Void> then(Task<Void> task) throws Exception {
// Retry the fetch as a save operation because this Installation was deleted on the server.
// Do not attempt to resave an object if LDS is enabled, since changing objectId is not allowed.
if(!Parse.isLocalDatastoreEnabled()
&& task.getError() != null
&& task.getError() instanceof ParseException
&& ((ParseException) task.getError()).getCode() == ParseException.OBJECT_NOT_FOUND) {
synchronized (mutex) {
setObjectId(null);
markAllFieldDirty();
return ParseInstallation.super.saveAsync(sessionToken, toAwait);
}
}
return task;
}
});
}

@Override
/* package */ Task<Void> handleSaveResultAsync(ParseObject.State result,
ParseOperationSet operationsBeforeSave) {
Expand Down
9 changes: 9 additions & 0 deletions Parse/src/main/java/com/parse/ParseObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -2815,6 +2815,15 @@ private void rebuildEstimatedData() {
}
}

/* package */ void markAllFieldDirty() {
synchronized (mutex) {
estimatedData.clear();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@natario1 will any changes be necessary for safeKeys() if we merge this in?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rogerhu no, it’s OK as it is now.

for (String key : state.keySet()) {
performPut(key, state.get(key));
}
}
}

/**
* performOperation() is like {@link #put(String, Object)} but instead of just taking a new value,
* it takes a ParseFieldOperation that modifies the value.
Expand Down
46 changes: 46 additions & 0 deletions Parse/src/test/java/com/parse/ParseInstallationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@
import bolts.Task;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
Expand Down Expand Up @@ -108,6 +110,50 @@ public void testImmutableKeys() {
}
}

@Test
public void testSaveAsync() throws Exception {
String sessionToken = "sessionToken";
Task<Void> toAwait = Task.forResult(null);

ParseCurrentInstallationController controller =
mock(ParseCurrentInstallationController.class);
ParseInstallation currentInstallation = new ParseInstallation();
when(controller.getAsync())
.thenReturn(Task.forResult(currentInstallation));
ParseCorePlugins.getInstance()
.registerCurrentInstallationController(controller);
ParseObject.State state = new ParseObject.State.Builder("_Installation")
.put("deviceToken", "deviceToken")
.build();
ParseInstallation installation = ParseInstallation.getCurrentInstallation();
assertNotNull(installation);
installation.setState(state);

ParseObjectController objController = mock(ParseObjectController.class);
// mock return task when Installation was deleted on the server
Task<ParseObject.State> taskError = Task.forError(new ParseException(ParseException.OBJECT_NOT_FOUND, ""));
// mock return task when Installation was re-saved to the server
Task<ParseObject.State> task = Task.forResult(null);
when(objController.saveAsync(
any(ParseObject.State.class),
any(ParseOperationSet.class),
eq(sessionToken),
any(ParseDecoder.class)))
.thenReturn(taskError)
.thenReturn(task);
ParseCorePlugins.getInstance()
.registerObjectController(objController);

installation.saveAsync(sessionToken, toAwait);

verify(controller).getAsync();
verify(objController, times(2)).saveAsync(
any(ParseObject.State.class),
any(ParseOperationSet.class),
eq(sessionToken),
any(ParseDecoder.class));
}

@Test
public void testHandleSaveResultAsync() throws Exception {
// Mock currentInstallationController to make setAsync work
Expand Down