Skip to content

Instantly share code, notes, and snippets.

@ericlewis
Created September 29, 2020 00:29
Show Gist options
  • Save ericlewis/5dcfffa21f29e806b763d93e809e177b to your computer and use it in GitHub Desktop.
Save ericlewis/5dcfffa21f29e806b763d93e809e177b to your computer and use it in GitHub Desktop.
ugh
func fetchOrder(type: FeedType) {
DispatchQueue.global(qos: .userInitiated).async {
let context = PersistenceController.shared.container.newBackgroundContext()
self.orderCancellable = self.ref.child(type.hackerNewsEndpoint).observeSingleEventPublisher(of: .value)
.compactMap {
$0.value as? [Int]
}
.flatMap { orders -> Publishers.Sequence<[Int], Never> in
// this may be a bad idea
DispatchQueue.main.async {
self.order = orders
}
return orders.publisher
}
.flatMap { id in
self.ref.child("item").child(String(id)).observeSingleEventPublisher(of: .value)
}
.compactMap {
$0.value as? NSDictionary
}
.map {
processItemDict($0, context: context)
}
.sink(receiveCompletion: { completion in
switch completion {
case .finished:
do {
try context.save()
} catch {
print(error)
}
case .failure(let error):
print(error)
}
}) { _ in
// values come out of this end, though they don't really need to
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment