|
27 | 27 | import com.amplifyframework.datastore.DataStoreChannelEventName; |
28 | 28 | import com.amplifyframework.datastore.appsync.ModelMetadata; |
29 | 29 | import com.amplifyframework.datastore.appsync.ModelWithMetadata; |
| 30 | +import com.amplifyframework.datastore.appsync.SerializedModel; |
30 | 31 | import com.amplifyframework.datastore.storage.LocalStorageAdapter; |
31 | 32 | import com.amplifyframework.datastore.storage.StorageItemChange; |
32 | 33 | import com.amplifyframework.hub.HubChannel; |
@@ -132,7 +133,7 @@ private <T extends Model> Completable delete(T model, Consumer<StorageItemChange |
132 | 133 | // First, check if the thing exists. |
133 | 134 | // If we don't, we'll get an exception saying basically, |
134 | 135 | // "failed to delete a non-existing thing." |
135 | | - ifPresent(model.getClass(), model.getId(), |
| 136 | + ifPresent(model, |
136 | 137 | () -> localStorageAdapter.delete( |
137 | 138 | model, |
138 | 139 | StorageItemChange.Initiator.SYNC_ENGINE, |
@@ -165,17 +166,20 @@ private <T extends Model> Completable save(T model, Consumer<StorageItemChange<T |
165 | 166 | } |
166 | 167 |
|
167 | 168 | /** |
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 |
172 | 172 | * @param onPresent If there is a match, perform this action |
173 | 173 | * @param onNotPresent If there is NOT a match, perform this action as a fallback |
174 | | - * @param <T> The type of item being searched |
175 | 174 | */ |
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 -> { |
179 | 183 | if (iterator.hasNext()) { |
180 | 184 | onPresent.call(); |
181 | 185 | } else { |
|
0 commit comments