combine

How can I branch out multiple API calls from the result of one API call and collect them after all are finished with Combine?

空扰寡人 提交于 2021-01-29 07:31:36
问题 So, I have this sequence of API calls, where I fetch a employee details, then fetch the company and project details that the employee is associated with. After both fetching are complete, I combine both and publish a fetchCompleted event. I've isolated the relevant code below. func getUserDetails() -> AnyPublisher<UserDetails, Error> func getCompanyDetails(user: UserDetails) -> AnyPublisher<CompanyDetails, Error> func getProjectDetails(user: UserDetails) -> AnyPublisher<ProjectDetails, Error>

Decode another response if first decoding failed using Combine and Swift

主宰稳场 提交于 2021-01-29 06:08:29
问题 I have the following model: struct Response: Decodable { let message: String } struct ErrorResponse: Decodable { let errorMessage: String } enum APIError: Error { case network(code: Int, description: String) case decoding(description: String) case api(description: String) } I'm trying to fetch an url and parse the JSON response using this flow: func fetch(url: URL) -> AnyPublisher<Response, APIError> { URLSession.shared.dataTaskPublisher(for: URLRequest(url: url)) // #1 URLRequest fails,

Loading image from remote URL asynchronously in SwiftUI Image using combine's Publisher

人走茶凉 提交于 2021-01-28 20:03:05
问题 I was looking for good solutions for loading images asynchronously from a remote server image URL. There were many solutions online. It's a shame Apple doesn't provide one natively for something that is so common. Anyways, I found Sundell's blog really interesting and took the good bits from it to create my own ImageLoader, as shown below: import Combine class ImageLoader { private let urlSession: URLSession private let cache: NSCache<NSURL, UIImage> init(urlSession: URLSession = .shared,

Combine Framework Update UI doesn't work properly

吃可爱长大的小学妹 提交于 2021-01-28 19:48:39
问题 I want to try Combine framework, very simple usage, press a UIButton , and update UILabel . My idea is: Add a publisher @Published var cacheText: String? Subscribe $cacheText.assign(to: \.text, on: cacheLabel) assign a value when button pressed. cacheText = "testString" Then the label's text should be updated. The problem is when the button pressed, the @Published value is updated, but the UILabel value doesn't change. e.g the cacheLabel1 was assigned 123 initially but not 789 when button

Alamofire + Combine + MVVM request example

孤街浪徒 提交于 2021-01-28 06:44:52
问题 in my view controller I have a property items to which I have a subscription and rendering my view. For this view controller I have a view model where I have load method like: @Published private(set) var items: [Item] = [] func load(params: [String: Any] = [:]) { self.isLoading = true self.subscription = WebRepo().index(params: params).sink(receiveCompletion: { [weak self] (completion) in switch completion { case .failure(let error): print("Error is: \(error.localizedDescription)") default:

Combine Future Publisher is not getting deallocated

不打扰是莪最后的温柔 提交于 2021-01-28 05:20:31
问题 I am using the Combine Future to wrap around an async block operation and adding a subscriber to that publisher to receive the values.. I am noticing the future object is not getting deallocated, even after the subscribers are deallocated. The XCode memory graph and instruments leaks graph itself shows no reference to these future objects. I am puzzled why are they still around. func getUsers(forceRefresh: Bool = false) -> AnyPublisher<[User], Error> { let future = Future<[User], Error> {

Combine turn one Publisher into another

橙三吉。 提交于 2021-01-21 04:11:30
问题 I use an OAuth framework which creates authenticated requests asynchronously like so: OAuthSession.current.makeAuthenticatedRequest(request: myURLRequest) { (result: Result<URLRequest, OAuthError>) in switch result { case .success(let request): URLSession.shared.dataTask(with: request) { (data, response, error) in // ... } // ... } } I am trying to make my OAuth framework use Combine, so I know have a Publisher version of the makeAuthenticatedRequest method i.e.: public func

Combine turn one Publisher into another

两盒软妹~` 提交于 2021-01-21 04:08:10
问题 I use an OAuth framework which creates authenticated requests asynchronously like so: OAuthSession.current.makeAuthenticatedRequest(request: myURLRequest) { (result: Result<URLRequest, OAuthError>) in switch result { case .success(let request): URLSession.shared.dataTask(with: request) { (data, response, error) in // ... } // ... } } I am trying to make my OAuth framework use Combine, so I know have a Publisher version of the makeAuthenticatedRequest method i.e.: public func

Combine turn one Publisher into another

孤街浪徒 提交于 2021-01-21 04:05:40
问题 I use an OAuth framework which creates authenticated requests asynchronously like so: OAuthSession.current.makeAuthenticatedRequest(request: myURLRequest) { (result: Result<URLRequest, OAuthError>) in switch result { case .success(let request): URLSession.shared.dataTask(with: request) { (data, response, error) in // ... } // ... } } I am trying to make my OAuth framework use Combine, so I know have a Publisher version of the makeAuthenticatedRequest method i.e.: public func

UserDefaults Binding with Toggle in SwiftUI

梦想的初衷 提交于 2021-01-21 01:33:14
问题 I'm trying to figure out the best way to build a simple settings screen bound to UserDefaults . Basically, I have a Toggle and I want: the value a UserDefault to be saved any time this Toggle is changed (the UserDefault should be the source of truth) the Toggle to always show the value of the UserDefault I have watched many of the SwiftUI WWDC sessions, but I'm still not sure exactly how I should set everything up with the different tools that are available within Combine and SwiftUI. My