Last active
February 13, 2022 09:46
-
-
Save JadenGeller/14e9815d9052f48fa4222cd500da21a3 to your computer and use it in GitHub Desktop.
Async function to fetch LPLinkMetadata
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Huh, the discussion comment here suggests that
startFetchingMetadata
is usable as async out of the box, so probably don't use this gist