Skip to content

Commit 9492c0a

Browse files
authored
fix: Delete relationships fix for flutter (#1168)
1 parent eaf9924 commit 9492c0a

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

aws-datastore/src/main/java/com/amplifyframework/datastore/storage/sqlite/SQLiteModelTree.java

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import com.amplifyframework.core.model.query.predicate.QueryPredicate;
3131
import com.amplifyframework.core.model.query.predicate.QueryPredicates;
3232
import com.amplifyframework.datastore.DataStoreException;
33+
import com.amplifyframework.datastore.appsync.SerializedModel;
3334
import com.amplifyframework.datastore.storage.sqlite.adapter.SQLiteTable;
3435
import com.amplifyframework.logging.Logger;
3536
import com.amplifyframework.util.Empty;
@@ -84,7 +85,7 @@ <T extends Model> List<Model> descendantsOf(Collection<T> root) {
8485
return new ArrayList<>();
8586
}
8687
Map<ModelSchema, Set<String>> modelMap = new LinkedHashMap<>();
87-
ModelSchema rootSchema = registry.getModelSchemaForModelInstance(root.iterator().next());
88+
ModelSchema rootSchema = registry.getModelSchemaForModelClass(getModelName(root.iterator().next()));
8889
Set<String> rootIds = new HashSet<>();
8990
for (T model : root) {
9091
rootIds.add(model.getId());
@@ -95,10 +96,18 @@ <T extends Model> List<Model> descendantsOf(Collection<T> root) {
9596
for (Map.Entry<ModelSchema, Set<String>> entry : modelMap.entrySet()) {
9697
ModelSchema schema = entry.getKey();
9798
for (String id : entry.getValue()) {
98-
// Create dummy model instance using just the ID and model type
99-
String dummyJson = gson.toJson(Collections.singletonMap("id", id));
100-
Model dummyItem = gson.fromJson(dummyJson, schema.getModelClass());
101-
descendants.add(dummyItem);
99+
if (root.iterator().next().getClass() == SerializedModel.class) {
100+
SerializedModel dummyItem = SerializedModel.builder()
101+
.serializedData(Collections.singletonMap("id", id))
102+
.modelSchema(schema)
103+
.build();
104+
descendants.add(dummyItem);
105+
} else {
106+
// Create dummy model instance using just the ID and model type
107+
String dummyJson = gson.toJson(Collections.singletonMap("id", id));
108+
Model dummyItem = gson.fromJson(dummyJson, schema.getModelClass());
109+
descendants.add(dummyItem);
110+
}
102111
}
103112
}
104113
return descendants;
@@ -172,4 +181,12 @@ private Cursor queryAll(
172181
final String[] bindings = sqlCommand.getBindingsAsArray();
173182
return database.rawQuery(rawQuery, bindings);
174183
}
184+
185+
private String getModelName(@NonNull Model model) {
186+
if (model.getClass() == SerializedModel.class) {
187+
return ((SerializedModel) model).getModelName();
188+
} else {
189+
return model.getClass().getSimpleName();
190+
}
191+
}
175192
}

aws-datastore/src/main/java/com/amplifyframework/datastore/storage/sqlite/SQLiteStorageAdapter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ public <T extends Model> void delete(
495495

496496
// publish cascaded deletions
497497
for (Model cascadedModel : cascadedModels) {
498-
ModelSchema schema = modelSchemaRegistry.getModelSchemaForModelInstance(cascadedModel);
498+
ModelSchema schema = modelSchemaRegistry.getModelSchemaForModelClass(getModelName(cascadedModel));
499499
itemChangeSubject.onNext(StorageItemChange.builder()
500500
.item(cascadedModel)
501501
.patchItem(SerializedModel.create(cascadedModel, schema))
@@ -575,7 +575,7 @@ public <T extends Model> void delete(
575575

576576
// publish every deletion
577577
for (Model model : modelsToDelete) {
578-
ModelSchema schema = modelSchemaRegistry.getModelSchemaForModelInstance(model);
578+
ModelSchema schema = modelSchemaRegistry.getModelSchemaForModelClass(getModelName(model));
579579
itemChangeSubject.onNext(StorageItemChange.builder()
580580
.item(model)
581581
.patchItem(SerializedModel.create(model, schema))

0 commit comments

Comments
 (0)