Last active
January 12, 2020 10:41
-
-
Save keremk/6f3a07ff0bbca6a425c17dc8b8b9e1ec 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 Foundation | |
import Combine | |
final class MovieCollection: ObservableObject { | |
@Published var movieResponse: MovieResponse? = nil | |
... | |
func fetchMovies() -> [Movie] { | |
let response: MovieResponse? = fetchPreviewData(previewFile: "movies.json", fetcher: fetchResponse) | |
return response?.results ?? [] | |
} | |
private func fetchResponse() -> MovieResponse? { | |
if movieResponse != nil { | |
return movieResponse | |
} | |
moviesFetchable?.fetchMovies() | |
.replaceError(with: MovieResponse.empty()) | |
.sink(receiveValue: { [weak self] movieResponse in | |
self?.movieResponse = movieResponse | |
}) | |
.store(in: &disposables) | |
return movieResponse | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment