Skip to content

Commit 82bc0d2

Browse files
fix: honor deletions from AppSync when used by Flutter (#1004)
When the DataStore is used from Flutter, Java-language model classes don't exist. The Merger component is responsible for applying remote mutations onto the local store. It contained an ifPresent() check, which checked for the presence of a model by its Java class. This method would fail to work as expected when the SerializedModel envelope type was used. ifPresent() has been updated to query the local store by model name, instead of using the Java class. Resolves: #994
1 parent 0c52bc8 commit 82bc0d2

File tree

1 file changed

+13
-9
lines changed
  • aws-datastore/src/main/java/com/amplifyframework/datastore/syncengine

1 file changed

+13
-9
lines changed

aws-datastore/src/main/java/com/amplifyframework/datastore/syncengine/Merger.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import com.amplifyframework.datastore.DataStoreChannelEventName;
2828
import com.amplifyframework.datastore.appsync.ModelMetadata;
2929
import com.amplifyframework.datastore.appsync.ModelWithMetadata;
30+
import com.amplifyframework.datastore.appsync.SerializedModel;
3031
import com.amplifyframework.datastore.storage.LocalStorageAdapter;
3132
import com.amplifyframework.datastore.storage.StorageItemChange;
3233
import com.amplifyframework.hub.HubChannel;
@@ -132,7 +133,7 @@ private <T extends Model> Completable delete(T model, Consumer<StorageItemChange
132133
// First, check if the thing exists.
133134
// If we don't, we'll get an exception saying basically,
134135
// "failed to delete a non-existing thing."
135-
ifPresent(model.getClass(), model.getId(),
136+
ifPresent(model,
136137
() -> localStorageAdapter.delete(
137138
model,
138139
StorageItemChange.Initiator.SYNC_ENGINE,
@@ -165,17 +166,20 @@ private <T extends Model> Completable save(T model, Consumer<StorageItemChange<T
165166
}
166167

167168
/**
168-
* If the DataStore contains an item of the given class and with the given ID,
169-
* then perform an action. Otherwise, perform some other action.
170-
* @param clazz Search for this class in the DataStore
171-
* @param modelId Search for an item with this ID in the DataStore
169+
* If the DataStore contains a model instance, then perform an action.
170+
* Otherwise, perform some other action.
171+
* @param model A model that might exist in local storage
172172
* @param onPresent If there is a match, perform this action
173173
* @param onNotPresent If there is NOT a match, perform this action as a fallback
174-
* @param <T> The type of item being searched
175174
*/
176-
private <T extends Model> void ifPresent(
177-
Class<T> clazz, String modelId, Action onPresent, Action onNotPresent) {
178-
localStorageAdapter.query(clazz, Where.id(modelId), iterator -> {
175+
private void ifPresent(Model model, Action onPresent, Action onNotPresent) {
176+
final String modelName;
177+
if (model instanceof SerializedModel) {
178+
modelName = ((SerializedModel) model).getModelName();
179+
} else {
180+
modelName = model.getClass().getSimpleName();
181+
}
182+
localStorageAdapter.query(modelName, Where.id(model.getId()), iterator -> {
179183
if (iterator.hasNext()) {
180184
onPresent.call();
181185
} else {

0 commit comments

Comments
 (0)