Skip to content

Commit 12d6277

Browse files
authored
Amplify Android: replace deprecated id with identifier method (aws-amplify#5613)
* replace deprecated id with identifier method * fix indentation * Fix formatting * Handle AmplifyException separately before delete * Handle AmplifyException separately before update * Handle AmplifyException separately before delete
1 parent e9fb02e commit 12d6277

File tree

6 files changed

+242
-134
lines changed

6 files changed

+242
-134
lines changed

src/fragments/lib-v1/datastore/android/data-access/delete-snippet.mdx

+38-20
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,34 @@ Below, you query for an instance with an `id` of `"123"`, and then delete it, if
44
<Block name="Java">
55

66
```java
7-
Amplify.DataStore.query(Post.class, Where.id("123"),
8-
matches -> {
9-
if (matches.hasNext()) {
10-
Post post = matches.next();
11-
Amplify.DataStore.delete(post,
12-
deleted -> Log.i("MyAmplifyApp", "Deleted a post."),
13-
failure -> Log.e("MyAmplifyApp", "Delete failed.", failure)
14-
);
15-
}
16-
},
17-
failure -> Log.e("MyAmplifyApp", "Query failed.", failure)
18-
);
7+
QueryOptions queryOptions = null;
8+
try {
9+
queryOptions = Where.identifier(Post.class, "123");
10+
} catch (AmplifyException e) {
11+
Log.e("MyAmplifyApp", "Failed to construct QueryOptions with provided values for Where.identifier", e);
12+
}
13+
14+
if (queryOptions != null) {
15+
Amplify.DataStore.query(Post.class, queryOptions,
16+
matches -> {
17+
if (matches.hasNext()) {
18+
Post post = matches.next();
19+
Amplify.DataStore.delete(post,
20+
deleted -> Log.i("MyAmplifyApp", "Deleted a post."),
21+
failure -> Log.e("MyAmplifyApp", "Delete failed.", failure)
22+
);
23+
}
24+
},
25+
failure -> Log.e("MyAmplifyApp", "Query failed.", failure)
26+
);
27+
}
1928
```
2029

2130
</Block>
2231
<Block name="Kotlin - Callbacks">
2332

2433
```kotlin
25-
Amplify.DataStore.query(Post::class.java, Where.id("123"),
34+
Amplify.DataStore.query(Post::class.java, Where.identifier(Post::class.java, "123"),
2635
{ matches ->
2736
if (matches.hasNext()) {
2837
val post = matches.next()
@@ -40,7 +49,7 @@ Amplify.DataStore.query(Post::class.java, Where.id("123"),
4049
<Block name="Kotlin - Coroutines">
4150

4251
```kotlin
43-
Amplify.DataStore.query(Post::class, Where.id("123"))
52+
Amplify.DataStore.query(Post::class, Where.identifier(Post::class.java, "123"))
4453
.catch { Log.e("MyAmplifyApp", "Query failed", it) }
4554
.onEach { Amplify.DataStore.delete(it) }
4655
.catch { Log.e("MyAmplifyApp", "Delete failed", it) }
@@ -51,12 +60,21 @@ Amplify.DataStore.query(Post::class, Where.id("123"))
5160
<Block name="RxJava">
5261

5362
```java
54-
RxAmplify.DataStore.query(Post.class, Where.id("123"))
55-
.flatMapCompletable(RxAmplify.DataStore::delete)
56-
.subscribe(
57-
() -> Log.i("MyAmplifyApp", "Deleted a post."),
58-
failure -> Log.e("MyAmplifyApp", "Delete failed.", failure)
59-
);
63+
QueryOptions queryOptions = null;
64+
try {
65+
queryOptions = Where.identifier(Post.class, "123");
66+
} catch (AmplifyException e) {
67+
Log.e("MyAmplifyApp", "Failed to construct QueryOptions with provided values for Where.identifier", e);
68+
}
69+
70+
if (queryOptions != null) {
71+
RxAmplify.DataStore.query(Post.class, queryOptions)
72+
.flatMapCompletable(RxAmplify.DataStore::delete)
73+
.subscribe(
74+
() -> Log.i("MyAmplifyApp", "Deleted a post."),
75+
failure -> Log.e("MyAmplifyApp", "Delete failed.", failure)
76+
);
77+
}
6078
```
6179

6280
</Block>

src/fragments/lib-v1/datastore/android/data-access/update-snippet.mdx

+45-27
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,37 @@
22
<Block name="Java">
33

44
```java
5-
Amplify.DataStore.query(Post.class, Where.id("123"),
6-
matches -> {
7-
if (matches.hasNext()) {
8-
Post original = matches.next();
9-
Post edited = original.copyOfBuilder()
10-
.title("New Title")
11-
.build();
12-
Amplify.DataStore.save(edited,
13-
updated -> Log.i("MyAmplifyApp", "Updated a post."),
14-
failure -> Log.e("MyAmplifyApp", "Update failed.", failure)
15-
);
16-
}
17-
},
18-
failure -> Log.e("MyAmplifyApp", "Query failed.", failure)
19-
);
5+
QueryOptions queryOptions = null;
6+
try {
7+
queryOptions = Where.identifier(Post.class, "123");
8+
} catch (AmplifyException e) {
9+
Log.e("MyAmplifyApp", "Failed to construct QueryOptions with provided values for Where.identifier", e);
10+
}
11+
12+
if (queryOptions != null) {
13+
Amplify.DataStore.query(Post.class, queryOptions,
14+
matches -> {
15+
if (matches.hasNext()) {
16+
Post original = matches.next();
17+
Post edited = original.copyOfBuilder()
18+
.title("New Title")
19+
.build();
20+
Amplify.DataStore.save(edited,
21+
updated -> Log.i("MyAmplifyApp", "Updated a post."),
22+
failure -> Log.e("MyAmplifyApp", "Update failed.", failure)
23+
);
24+
}
25+
},
26+
failure -> Log.e("MyAmplifyApp", "Query failed.", failure)
27+
);
28+
}
2029
```
2130

2231
</Block>
2332
<Block name="Kotlin - Callbacks">
2433

2534
```kotlin
26-
Amplify.DataStore.query(Post::class.java, Where.id("123"),
35+
Amplify.DataStore.query(Post::class.java, Where.identifier(Post::class.java, "123"),
2736
{ matches ->
2837
if (matches.hasNext()) {
2938
val original = matches.next()
@@ -44,7 +53,7 @@ Amplify.DataStore.query(Post::class.java, Where.id("123"),
4453
<Block name="Kotlin - Coroutines">
4554

4655
```kotlin
47-
Amplify.DataStore.query(Post::class, Where.id("123"))
56+
Amplify.DataStore.query(Post::class, Where.identifier(Post::class.java, "123"))
4857
.catch { Log.e("MyAmplifyApp", "Query failed", it) }
4958
.map { it.copyOfBuilder().title("New Title").build() }
5059
.onEach { Amplify.DataStore.save(it) }
@@ -56,16 +65,25 @@ Amplify.DataStore.query(Post::class, Where.id("123"))
5665
<Block name="RxJava">
5766

5867
```java
59-
RxAmplify.DataStore.query(Post.class, Where.id("123"))
60-
.map(matchingPost -> matchingPost.copyOfBuilder()
61-
.title("New Title")
62-
.build()
63-
)
64-
.flatMapCompletable(RxAmplify.DataStore::save)
65-
.subscribe(
66-
() -> Log.i("MyAmplifyApp", "Query and update succeeded."),
67-
failure -> Log.e("MyAmplifyApp", "Update failed.", failure)
68-
);
68+
QueryOptions queryOptions = null;
69+
try {
70+
queryOptions = Where.identifier(Post.class, "123");
71+
} catch (AmplifyException e) {
72+
Log.e("MyAmplifyApp", "Failed to construct QueryOptions with provided values for Where.identifier", e);
73+
}
74+
75+
if (queryOptions != null) {
76+
RxAmplify.DataStore.query(Post.class, queryOptions)
77+
.map(matchingPost -> matchingPost.copyOfBuilder()
78+
.title("New Title")
79+
.build()
80+
)
81+
.flatMapCompletable(RxAmplify.DataStore::save)
82+
.subscribe(
83+
() -> Log.i("MyAmplifyApp", "Query and update succeeded."),
84+
failure -> Log.e("MyAmplifyApp", "Update failed.", failure)
85+
);
86+
}
6987
```
7088

7189
</Block>

src/fragments/lib-v1/datastore/android/relational/delete-snippet.mdx

+38-20
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,34 @@
22
<Block name="Java">
33

44
```java
5-
Amplify.DataStore.query(Post.class, Where.id("123"),
6-
match -> {
7-
if (match.hasNext()) {
8-
Post post = match.next();
9-
Amplify.DataStore.delete(post,
10-
deleted -> Log.i("MyAmplifyApp", "Post deleted."),
11-
failure -> Log.e("MyAmplifyApp", "Delete failed.", failure)
12-
);
13-
}
14-
},
15-
failure -> Log.e("MyAmplifyApp", "Query failed.", failure)
16-
);
5+
QueryOptions queryOptions = null;
6+
try {
7+
queryOptions = Where.identifier(Post.class, "123");
8+
} catch (AmplifyException e) {
9+
Log.e("MyAmplifyApp", "Failed to construct QueryOptions with provided values for Where.identifier", e);
10+
}
11+
12+
if (queryOptions != null) {
13+
Amplify.DataStore.query(Post.class, queryOptions,
14+
match -> {
15+
if (match.hasNext()) {
16+
Post post = match.next();
17+
Amplify.DataStore.delete(post,
18+
deleted -> Log.i("MyAmplifyApp", "Post deleted."),
19+
failure -> Log.e("MyAmplifyApp", "Delete failed.", failure)
20+
);
21+
}
22+
},
23+
failure -> Log.e("MyAmplifyApp", "Query failed.", failure)
24+
);
25+
}
1726
```
1827

1928
</Block>
2029
<Block name="Kotlin - Callbacks">
2130

2231
```kotlin
23-
Amplify.DataStore.query(Post::class.java, Where.id("123"),
32+
Amplify.DataStore.query(Post::class.java, Where.identifier(Post::class.java, "123"),
2433
{
2534
if (it.hasNext()) {
2635
val post = it.next()
@@ -38,7 +47,7 @@ Amplify.DataStore.query(Post::class.java, Where.id("123"),
3847
<Block name="Kotlin - Coroutines">
3948

4049
```kotlin
41-
Amplify.DataStore.query(Post::class, Where.id("123"))
50+
Amplify.DataStore.query(Post::class, Where.identifier(Post::class.java, "123"))
4251
.catch { Log.e("MyAmplifyApp", "Query failed", it) }
4352
.onEach { Amplify.DataStore.delete(it) }
4453
.catch { Log.e("MyAmplifyApp", "Delete failed", it) }
@@ -49,12 +58,21 @@ Amplify.DataStore.query(Post::class, Where.id("123"))
4958
<Block name="RxJava">
5059

5160
```java
52-
RxAmplify.DataStore.query(Post.class, Where.id("123"))
53-
.flatMapCompletable(RxAmplify.DataStore::delete)
54-
.subscribe(
55-
() -> Log.i("MyAmplifyApp", "Post deleted."),
56-
failure -> Log.e("MyAmplifyApp", "Delete failed.", failure)
57-
);
61+
QueryOptions queryOptions = null;
62+
try {
63+
queryOptions = Where.identifier(Post.class, "123");
64+
} catch (AmplifyException e) {
65+
Log.e("MyAmplifyApp", "Failed to construct QueryOptions with provided values for Where.identifier", e);
66+
}
67+
68+
if (queryOptions != null) {
69+
RxAmplify.DataStore.query(Post.class, queryOptions)
70+
.flatMapCompletable(RxAmplify.DataStore::delete)
71+
.subscribe(
72+
() -> Log.i("MyAmplifyApp", "Post deleted."),
73+
failure -> Log.e("MyAmplifyApp", "Delete failed.", failure)
74+
);
75+
}
5876
```
5977

6078
</Block>

src/fragments/lib/datastore/android/data-access/delete-snippet.mdx

+38-20
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,34 @@ Below, you query for an instance with an `id` of `"123"`, and then delete it, if
44
<Block name="Java">
55

66
```java
7-
Amplify.DataStore.query(Post.class, Where.id("123"),
8-
matches -> {
9-
if (matches.hasNext()) {
10-
Post post = matches.next();
11-
Amplify.DataStore.delete(post,
12-
deleted -> Log.i("MyAmplifyApp", "Deleted a post."),
13-
failure -> Log.e("MyAmplifyApp", "Delete failed.", failure)
14-
);
15-
}
16-
},
17-
failure -> Log.e("MyAmplifyApp", "Query failed.", failure)
18-
);
7+
QueryOptions queryOptions = null;
8+
try {
9+
queryOptions = Where.identifier(Post.class, "123");
10+
} catch (AmplifyException e) {
11+
Log.e("MyAmplifyApp", "Failed to construct QueryOptions with provided values for Where.identifier", e);
12+
}
13+
14+
if (queryOptions != null) {
15+
Amplify.DataStore.query(Post.class, queryOptions,
16+
matches -> {
17+
if (matches.hasNext()) {
18+
Post post = matches.next();
19+
Amplify.DataStore.delete(post,
20+
deleted -> Log.i("MyAmplifyApp", "Deleted a post."),
21+
failure -> Log.e("MyAmplifyApp", "Delete failed.", failure)
22+
);
23+
}
24+
},
25+
failure -> Log.e("MyAmplifyApp", "Query failed.", failure)
26+
);
27+
}
1928
```
2029

2130
</Block>
2231
<Block name="Kotlin - Callbacks">
2332

2433
```kotlin
25-
Amplify.DataStore.query(Post::class.java, Where.id("123"),
34+
Amplify.DataStore.query(Post::class.java, Where.identifier(Post::class.java, "123"),
2635
{ matches ->
2736
if (matches.hasNext()) {
2837
val post = matches.next()
@@ -40,7 +49,7 @@ Amplify.DataStore.query(Post::class.java, Where.id("123"),
4049
<Block name="Kotlin - Coroutines">
4150

4251
```kotlin
43-
Amplify.DataStore.query(Post::class, Where.id("123"))
52+
Amplify.DataStore.query(Post::class, Where.identifier(Post::class.java, "123"))
4453
.catch { Log.e("MyAmplifyApp", "Query failed", it) }
4554
.onEach { Amplify.DataStore.delete(it) }
4655
.catch { Log.e("MyAmplifyApp", "Delete failed", it) }
@@ -51,12 +60,21 @@ Amplify.DataStore.query(Post::class, Where.id("123"))
5160
<Block name="RxJava">
5261

5362
```java
54-
RxAmplify.DataStore.query(Post.class, Where.id("123"))
55-
.flatMapCompletable(RxAmplify.DataStore::delete)
56-
.subscribe(
57-
() -> Log.i("MyAmplifyApp", "Deleted a post."),
58-
failure -> Log.e("MyAmplifyApp", "Delete failed.", failure)
59-
);
63+
QueryOptions queryOptions = null;
64+
try {
65+
queryOptions = Where.identifier(Post.class, "123");
66+
} catch (AmplifyException e) {
67+
Log.e("MyAmplifyApp", "Failed to construct QueryOptions with provided values for Where.identifier", e);
68+
}
69+
70+
if (queryOptions != null) {
71+
RxAmplify.DataStore.query(Post.class, queryOptions)
72+
.flatMapCompletable(RxAmplify.DataStore::delete)
73+
.subscribe(
74+
() -> Log.i("MyAmplifyApp", "Deleted a post."),
75+
failure -> Log.e("MyAmplifyApp", "Delete failed.", failure)
76+
);
77+
}
6078
```
6179

6280
</Block>

0 commit comments

Comments
 (0)