combine

Difference between CurrentValueSubject and @Published

旧城冷巷雨未停 提交于 2021-02-18 22:10:53
问题 So I'm digging into combine and this question came up. Is there any real difference between using CurrentValueSubject (and setting its value using currentValueSubject.value ) or using a @Published var and accessing its publisher with a $ ? I mean I know one returns a Subject instead of a Publisher , but the only real difference I could find is that CurrentValueSubject is way more useful because you can declare it on a protocol. I really don't understand how @Published can be useful if we can

Setting a SwiftUI @EnvironmentObject from outside a view

假如想象 提交于 2021-02-16 13:58:12
问题 I'd like to have a worker task update a SwiftUI view. The worker task is busy doing the procedural work of the application - playing sounds, and firing timer-based events. I'd like to to flash several icons in a SwiftUI view during those timer events. So I want to trigger a view refresh in those icon views. So, I created an environmentObject called Settings. It's instantiated in the App Delegate, and attached to the root view in the SceneDelegate. The Settings object works just fine inside

UI changes with ObservableObject just after switching tabs

送分小仙女□ 提交于 2021-02-11 16:51:33
问题 I have a ObservableObject that I use to update my UI when new data is sent from the server (an a class which contains an array of custom structs). For some reason, when the data is sent, the ContentView 's body is called, but the data isn't changed. I even added a print statement to check if the data that the array contains is right and it is. When I try to switch to another tab on my TabView , and then switch back to the main view, the UI does get updated. Does anybody know why the UI

UI changes with ObservableObject just after switching tabs

烈酒焚心 提交于 2021-02-11 16:51:24
问题 I have a ObservableObject that I use to update my UI when new data is sent from the server (an a class which contains an array of custom structs). For some reason, when the data is sent, the ContentView 's body is called, but the data isn't changed. I even added a print statement to check if the data that the array contains is right and it is. When I try to switch to another tab on my TabView , and then switch back to the main view, the UI does get updated. Does anybody know why the UI

UI changes with ObservableObject just after switching tabs

若如初见. 提交于 2021-02-11 16:50:43
问题 I have a ObservableObject that I use to update my UI when new data is sent from the server (an a class which contains an array of custom structs). For some reason, when the data is sent, the ContentView 's body is called, but the data isn't changed. I even added a print statement to check if the data that the array contains is right and it is. When I try to switch to another tab on my TabView , and then switch back to the main view, the UI does get updated. Does anybody know why the UI

UI changes with ObservableObject just after switching tabs

怎甘沉沦 提交于 2021-02-11 16:49:36
问题 I have a ObservableObject that I use to update my UI when new data is sent from the server (an a class which contains an array of custom structs). For some reason, when the data is sent, the ContentView 's body is called, but the data isn't changed. I even added a print statement to check if the data that the array contains is right and it is. When I try to switch to another tab on my TabView , and then switch back to the main view, the UI does get updated. Does anybody know why the UI

What's the proper syntax of mapping/filtering a data-stream element to convert its data type?

早过忘川 提交于 2021-02-11 14:10:59
问题 Scenario: Data stream that contains an item that has changed from an Int to String type causing the JSON parser to crash. Result: Subscriber 'sink' crashed with data type not matching the original receiving type via JSON parser. Goal: to convert the Int values to String to have a consistent stream for a successful parsing. Here's a snippet of the data stream that has caused the crash: ... { "city": "אלון שבות", "sickCount": 124, "actualSick": 15, "verifiedLast7Days": " 11-14 ", "testLast7Days

Unable to get the response from URL using combine with SwiftUI

家住魔仙堡 提交于 2021-02-10 17:56:15
问题 Thats my model class struct LoginResponse: Codable { let main: LoginModel } struct LoginModel: Codable { let success: Bool? let token: String? let message: String? static var placeholder: LoginModel { return LoginModel(success: nil, token: nil, message: nil) } } Thats my service. I have one more issue i am using two map here but when try to remove map.data getting error in dataTaskPublisher. error mention below Instance method 'decode(type:decoder:)' requires the types 'URLSession

“some Protocol” causes type to not conform to protocol

主宰稳场 提交于 2021-02-10 17:50:22
问题 I don't understand why this doesn't compile. It does if I remove the where restriction from the P type. import Combine protocol Foo { associatedtype P: Publisher where P.Output == Int var publisher: P { get } } struct Bar: Foo { var publisher: some Publisher { Just(1) } } The error says that Type 'Bar' does not conform to protocol 'Foo' . I guess it's because publisher return type is not just any some Publisher . But in SwiftUI, the View uses a similar approach, just that it doesn't have

“some Protocol” causes type to not conform to protocol

我是研究僧i 提交于 2021-02-10 17:48:56
问题 I don't understand why this doesn't compile. It does if I remove the where restriction from the P type. import Combine protocol Foo { associatedtype P: Publisher where P.Output == Int var publisher: P { get } } struct Bar: Foo { var publisher: some Publisher { Just(1) } } The error says that Type 'Bar' does not conform to protocol 'Foo' . I guess it's because publisher return type is not just any some Publisher . But in SwiftUI, the View uses a similar approach, just that it doesn't have