combine

Swift Combine: Buffer upstream values and emit them at a steady rate?

让人想犯罪 __ 提交于 2021-02-06 09:03:44
问题 Using the new Combine framework in iOS 13. Suppose I have an upstream publisher sending values at a highly irregular rate - sometimes seconds or minutes may go by without any values, and then a stream of values may come through all at once. I'd like to create a custom publisher that subscribes to the upstream values, buffers them and emits them at a regular, known cadence when they come in, but publishes nothing if they've all been exhausted. For a concrete example: t = 0 to 5000ms: no

Reference EnvironmentObject in ObservableObject

こ雲淡風輕ζ 提交于 2021-02-05 04:58:51
问题 I have a LoginView that shows a RegisterView if the user is not logged in, and a ContentView if he is logged in: struct LoginView: View { @EnvironmentObject var userManager: UserManager var body: some View { Group { if userManager.isRegistered { ContentView() } else { RegisterView() } } } } ContentView have three ObservedObject properties, that uses combine to fetch content from a server with rest api's. struct ContentView: View { @EnvironmentObject var userManager: UserManager

Reference EnvironmentObject in ObservableObject

流过昼夜 提交于 2021-02-05 04:57:39
问题 I have a LoginView that shows a RegisterView if the user is not logged in, and a ContentView if he is logged in: struct LoginView: View { @EnvironmentObject var userManager: UserManager var body: some View { Group { if userManager.isRegistered { ContentView() } else { RegisterView() } } } } ContentView have three ObservedObject properties, that uses combine to fetch content from a server with rest api's. struct ContentView: View { @EnvironmentObject var userManager: UserManager

Reference EnvironmentObject in ObservableObject

假装没事ソ 提交于 2021-02-05 04:57:38
问题 I have a LoginView that shows a RegisterView if the user is not logged in, and a ContentView if he is logged in: struct LoginView: View { @EnvironmentObject var userManager: UserManager var body: some View { Group { if userManager.isRegistered { ContentView() } else { RegisterView() } } } } ContentView have three ObservedObject properties, that uses combine to fetch content from a server with rest api's. struct ContentView: View { @EnvironmentObject var userManager: UserManager

Reference EnvironmentObject in ObservableObject

谁都会走 提交于 2021-02-05 04:56:25
问题 I have a LoginView that shows a RegisterView if the user is not logged in, and a ContentView if he is logged in: struct LoginView: View { @EnvironmentObject var userManager: UserManager var body: some View { Group { if userManager.isRegistered { ContentView() } else { RegisterView() } } } } ContentView have three ObservedObject properties, that uses combine to fetch content from a server with rest api's. struct ContentView: View { @EnvironmentObject var userManager: UserManager

Swift Combine sink stops receiving values after first error

泪湿孤枕 提交于 2021-02-04 07:41:04
问题 Im moving my project to Combine from RxSwift I have a logic where I want publisher to emit event every time I click button. Acrually clicking button executed pushMe.send() pushMe .print("Debug") .flatMap { (res) -> AnyPublisher<Bool, Error> in return Future<Bool, Error>.init { closure in closure(.failure(Errors.validationFail)) }.eraseToAnyPublisher() } .sink(receiveCompletion: { completion in print("Completion received") }, receiveValue: { value in print("Value = \(value)") }) .store(in:

Swift Combine sink stops receiving values after first error

六月ゝ 毕业季﹏ 提交于 2021-02-04 07:39:47
问题 Im moving my project to Combine from RxSwift I have a logic where I want publisher to emit event every time I click button. Acrually clicking button executed pushMe.send() pushMe .print("Debug") .flatMap { (res) -> AnyPublisher<Bool, Error> in return Future<Bool, Error>.init { closure in closure(.failure(Errors.validationFail)) }.eraseToAnyPublisher() } .sink(receiveCompletion: { completion in print("Completion received") }, receiveValue: { value in print("Value = \(value)") }) .store(in:

SwiftUI toggle() function on Published values stopped triggering didSet with Swift 5.2

筅森魡賤 提交于 2021-01-29 19:08:49
问题 I have just updated my Xcode to 11.4 from 11.3 and my project written in SwiftUI started to behave differently. I used to call toggle() function for boolean values and it used to trigger didSet property observer, however, it is not working any more. Let' say we have a State property called isSettingOn . I used to call this: isSettingOn.toggle() which was triggering didSet observer of the property. Now, only if I call this: isSettingOn = true it is working. My projects are all based on this

Combine Future block called multiple times when using Flatmap and multiple subscribers

和自甴很熟 提交于 2021-01-29 08:56:44
问题 I've been successfully using BrightFutures in my apps mainly for async network requests. I decided it was time to see if I could migrate to Combine. However what I find is that when I combine two Futures using flatMap with two subscribers my second Future code block is executed twice. Here's some example code which will run directly in a playground: import Combine import Foundation extension Publisher { func showActivityIndicatorWhileWaiting(message: String) -> AnyCancellable { let

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:40:47
问题 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>