Forked from ericlewis/LPMetadataProvider+Combine.swift
Created
January 15, 2021 11:27
-
-
Save sergeytimoshin/beec803a0139624fa9399d003db353d0 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
import Combine | |
extension LPMetadataProvider { | |
func startFetchingMetadataPublisher(for url: URL) -> AnyPublisher<LPLinkMetadata?, Error> { | |
Future<LPLinkMetadata?, Error> { completion in | |
self.startFetchingMetadata(for: url) { meta, error in | |
guard let error = error else { | |
return completion(.success(meta)) | |
} | |
completion(.failure(error)) | |
} | |
} | |
.handleEvents(receiveCancel: { | |
self.cancel() | |
}) | |
.eraseToAnyPublisher() | |
} | |
} | |
let provider = LPMetadataProvider() | |
let cancellable: AnyCancellable = provider.startFetchingMetadataPublisher(for: URL(string: "https://apple.com")!) | |
.receive(on: RunLoop.main) | |
.sink(receiveCompletion: { completion in }) { | |
print($0) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment