Skip to content

Instantly share code, notes, and snippets.

@JadenGeller
Last active February 13, 2022 09:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JadenGeller/14e9815d9052f48fa4222cd500da21a3 to your computer and use it in GitHub Desktop.
Save JadenGeller/14e9815d9052f48fa4222cd500da21a3 to your computer and use it in GitHub Desktop.
Async function to fetch LPLinkMetadata
import LinkPresentation
extension LPLinkMetadata {
static func fetching(for url: URL) async throws -> LPLinkMetadata {
let provider = LPMetadataProvider()
return try await withTaskCancellationHandler {
try await withCheckedThrowingContinuation { continuation in
provider.startFetchingMetadata(for: url) { metadata, error in
switch (metadata, error) {
case (nil, let error?):
continuation.resume(throwing: error)
case (let output?, nil):
continuation.resume(returning: output)
case (_?, _?):
preconditionFailure("Expected nil metadata or nil error, but both were some")
case (nil, nil):
preconditionFailure("Expected some metadata or some error, but both were nil")
}
}
}
} onCancel: {
provider.cancel()
}
}
}
@JadenGeller
Copy link
Author

Huh, the discussion comment here suggests that startFetchingMetadata is usable as async out of the box, so probably don't use this gist

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment