Skip to content

Commit 7c65cb8

Browse files
committed
Use defer to cleanup
1 parent 77dc42b commit 7c65cb8

File tree

1 file changed

+27
-10
lines changed
  • swift/example_code/identity-resolvers/cognito-resolver/Sources

1 file changed

+27
-10
lines changed

swift/example_code/identity-resolvers/cognito-resolver/Sources/Example.swift

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,16 @@ class Example {
1616

1717
var cognitoIdentityClient: CognitoIdentityClient!
1818
var iamClient: IAMClient!
19+
var roleName: String
1920

21+
/// The name of the AWS Cognito Identity Pool to use.
2022
let identityPoolName: String
23+
/// The ID of the Identity Pool.
2124
var identityPoolID: String!
22-
var roleName: String
2325

26+
/// The name of the managed policy granting Amazon S3 permissions.
2427
let managedPolicyName: String
28+
/// The ARN of the managed policy granting S3 permissions.
2529
var managedPolicyArn: String?
2630

2731
/// Initialize the example.
@@ -48,6 +52,16 @@ class Example {
4852
///
4953
/// - Throws: Errors from IAM, STS, or Cognito.
5054
func run() async throws {
55+
// Set up the cleanup function to run automatically when this object
56+
// is discarded. This way, we clean up AWS artifacts whether the run
57+
// is successful or an error occurs.
58+
59+
defer {
60+
blocking {
61+
await self.cleanup()
62+
}
63+
}
64+
5165
// Create an identity pool to use for this example.
5266

5367
print("Creating a Cognito identity pool named \(identityPoolName)...")
@@ -84,7 +98,6 @@ class Example {
8498

8599
guard let role = createRoleOutput.role else {
86100
print("*** No role returned by CreateRole!")
87-
await cleanup()
88101
return
89102
}
90103

@@ -158,7 +171,6 @@ class Example {
158171

159172
guard let managedPolicy = createPolicyOutput.policy else {
160173
print("No policy returned by CreatePolicy!")
161-
await cleanup()
162174
return
163175
}
164176

@@ -193,20 +205,13 @@ class Example {
193205
)
194206
guard let buckets = listBucketsOutput.buckets else {
195207
print("No buckets returned by S3!")
196-
await cleanup()
197208
return
198209
}
199210

200211
print("Found \(buckets.count) S3 buckets:")
201212
for bucket in buckets {
202213
print(" \(bucket.name ?? "<unnamed>")")
203214
}
204-
205-
//======================================================================
206-
// Clean up before exiting.
207-
//======================================================================
208-
209-
await cleanup()
210215
}
211216

212217
/// Clean up by deleting AWS assets created by the example. Ignores
@@ -229,4 +234,16 @@ class Example {
229234
input: DeleteRoleInput(roleName: roleName)
230235
)
231236
}
237+
238+
/// Create a function that blocks the caller until execution is complete.
239+
///
240+
/// - Parameter block: The function to call and wait for its return.
241+
private func blocking(_ block: @escaping @Sendable () async -> Void) {
242+
let semaphore = DispatchSemaphore(value: 0)
243+
Task {
244+
await block()
245+
semaphore.signal()
246+
}
247+
semaphore.wait()
248+
}
232249
}

0 commit comments

Comments
 (0)