Post

Replies

Boosts

Views

Activity

Reply to Combining More Than Four @Published Variables in Combine?
I guess it goes like the following. Publishers.CombineLatest4($variable0, $variable1, $variable2, $variable3) .combineLatest($variable4) .combineLatest($variable5) .sink { completion in } receiveValue: { response0, response1 in let variable = response0.0 let variable4 = response0.1 let variable5 = response1 let v0 = variable.0 let v1 = variable.1 let v2 = variable.2 let v3 = variable.3 }.store(in: &cancellables) It's kind of odd.
Topic: UI Frameworks SubTopic: UIKit Tags:
Nov ’21
Reply to Using URLSession in Combine
Okay... The solution to my question is use of the Future guy. class APIClient: NSObject { var cancellables = [AnyCancellable]() @Published var models = [MyModel]() func fetchData(urlStr: String) -> Future<[MyModel], Error> { return Future<[MyModel], Error> { [weak self] promise in guard let url = URL(string: urlStr) else { return promise(.failure("Failure" as! Error)) } guard let strongSelf = self else { return } URLSession.shared.dataTaskPublisher(for: url) .map { $0.data } .decode(type: [MyModel].self, decoder: JSONDecoder()) .replaceError(with: []) .sink { completion in if case .failure(let error) = completion { promise(.failure(error)) } } receiveValue: { promise(.success($0)) } .store(in: &strongSelf.cancellables) } } } class ViewModel: NSObject { @IBOutlet var apiClient: APIClient! var cancellables = Set<AnyCancellable>() @Published var dataModels = [MyModel]() func getGitData() -> Future<[MyModel], Error> { let urlStr = "https://api.github.com/repos/ReactiveX/RxSwift/events" return Future<[MyModel], Error> { [weak self] promise in guard let strongSelf = self else { return } strongSelf.apiClient.fetchData(urlStr: urlStr) .sink { completion in if case .failure(let error) = completion { promise(.failure(error)) } } receiveValue: { promise(.success($0)) print("view model: \($0.count)") } .store(in: &strongSelf.cancellables) } } }
Topic: UI Frameworks SubTopic: UIKit Tags:
Nov ’21
Reply to Observing Changes in Multiple @Published Variables at the Same Time?
I think I've figured it out for myself. It looks like I can use Publishers.CombineLatest(,) as follows. class ViewController: UIViewController { var cancellables = Set<AnyCancellable>() @Published var userText: String = "" @Published var passText: String = "" override func viewDidLoad() { super.viewDidLoad() NotificationCenter.default.publisher(for: UITextField.textDidChangeNotification, object: usernameTextField) .sink(receiveValue: { (result) in if let myField = result.object as? UITextField { if let text = myField.text { self.userText = text } } }) .store(in: &cancellables) NotificationCenter.default.publisher(for: UITextField.textDidChangeNotification, object: passwordTextField) .sink(receiveValue: { (result) in if let myField = result.object as? UITextField { if let text = myField.text { self.passText = text } } }) .store(in: &cancellables) Publishers.CombineLatest($userText, $passText) .sink { (result0, result1) in print(result0, result1) }.store(in: &cancellables) } }
Topic: UI Frameworks SubTopic: UIKit Tags:
Oct ’21