combine

Why does User Defaults publisher trigger multiple times

冷暖自知 提交于 2021-02-10 05:14:53
问题 I'm subscribing the the built-in User Defaults extension, but it seems to be firing multiple times unnecessarily. This is the code I'm using: import Combine import Foundation import PlaygroundSupport extension UserDefaults { @objc var someProperty: Bool { get { bool(forKey: "someProperty") } set { set(newValue, forKey: "someProperty") } } } let defaults = UserDefaults.standard defaults.dictionaryRepresentation().keys .forEach(defaults.removeObject) print("Before: \(defaults.someProperty)")

Why does User Defaults publisher trigger multiple times

半腔热情 提交于 2021-02-10 05:14:39
问题 I'm subscribing the the built-in User Defaults extension, but it seems to be firing multiple times unnecessarily. This is the code I'm using: import Combine import Foundation import PlaygroundSupport extension UserDefaults { @objc var someProperty: Bool { get { bool(forKey: "someProperty") } set { set(newValue, forKey: "someProperty") } } } let defaults = UserDefaults.standard defaults.dictionaryRepresentation().keys .forEach(defaults.removeObject) print("Before: \(defaults.someProperty)")

Why does User Defaults publisher trigger multiple times

二次信任 提交于 2021-02-10 05:13:34
问题 I'm subscribing the the built-in User Defaults extension, but it seems to be firing multiple times unnecessarily. This is the code I'm using: import Combine import Foundation import PlaygroundSupport extension UserDefaults { @objc var someProperty: Bool { get { bool(forKey: "someProperty") } set { set(newValue, forKey: "someProperty") } } } let defaults = UserDefaults.standard defaults.dictionaryRepresentation().keys .forEach(defaults.removeObject) print("Before: \(defaults.someProperty)")

Why does User Defaults publisher trigger multiple times

落爺英雄遲暮 提交于 2021-02-10 05:13:32
问题 I'm subscribing the the built-in User Defaults extension, but it seems to be firing multiple times unnecessarily. This is the code I'm using: import Combine import Foundation import PlaygroundSupport extension UserDefaults { @objc var someProperty: Bool { get { bool(forKey: "someProperty") } set { set(newValue, forKey: "someProperty") } } } let defaults = UserDefaults.standard defaults.dictionaryRepresentation().keys .forEach(defaults.removeObject) print("Before: \(defaults.someProperty)")

SwiftUI + Combine, using Models and ViewModels together

我的未来我决定 提交于 2021-02-08 10:18:17
问题 I'm very new to Swift and I am currently trying to learn by building a rent splitting app with SwiftUI + Combine. I want to follow the MVVM pattern and am trying to implement this. At the moment I have the following Model, ViewModel and View files: Model: import Foundation import Combine struct InputAmounts { var myMonthlyIncome : Double var housemateMonthlyIncome : Double var totalRent : Double } ViewModel (where I have attempted to use the data from the Model to conform to the MVVM pattern,

SwiftUI - Optional Timer, reset and recreate

左心房为你撑大大i 提交于 2021-02-08 08:21:29
问题 Normally, I would use an optional variable to hold my Timer reference, as it's nice to be able to invalidate and set it to nil before recreating. I'm trying to use SwiftUI and want to make sure I'm correctly doing so... I declare as: @State var timer:Publishers.Autoconnect<Timer.TimerPublisher>? = nil Later I: self.timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect() To drive a UI text control I use: .onReceive(timer) { time in print("The time is now \(time)") } What is the

SwiftUI updating UI with high frequency data

▼魔方 西西 提交于 2021-02-08 06:51:30
问题 I'm trying to update the main view with high frequency data coming from separate background thread. I've created two tabviews and in case of slow update rate I can change the view. But in another case the UI doesn't react. I've observed this behavior only on real device, in the simulator works everything fine. The while loop is still representing an imu, just to keep it simple. Did someone any idea how to fix this issue? Many thanks! import SwiftUI struct ContentView: View {

SwiftUI updating UI with high frequency data

守給你的承諾、 提交于 2021-02-08 06:51:21
问题 I'm trying to update the main view with high frequency data coming from separate background thread. I've created two tabviews and in case of slow update rate I can change the view. But in another case the UI doesn't react. I've observed this behavior only on real device, in the simulator works everything fine. The while loop is still representing an imu, just to keep it simple. Did someone any idea how to fix this issue? Many thanks! import SwiftUI struct ContentView: View {

How to use Combine framework NSObject.KeyValueObservingPublisher?

℡╲_俬逩灬. 提交于 2021-02-07 12:54:23
问题 I'm trying to use the Combine framework NSObject.KeyValueObservingPublisher. I can see how to produce this publisher by calling publisher(for:options:) on an NSObject. But I'm having two problems: I can include .old in the options , but no .old value ever arrives. The only values that appear are the .initial value (when we subscribe) and the .new value (each time the observed property changes). I can suppress the .initial value but I can't suppress the .new value or add the .old value. If the

How can I get data from ObservedObject with onReceive in SwiftUI?

拈花ヽ惹草 提交于 2021-02-07 05:01:56
问题 In my SwiftUI app, I need to get data from ObservedObject each time the value change. I understood that we could do that with .onReceive? I don't understand well the documentation of Apple about it. I don't know how I can do this. My code: import SwiftUI import CoreLocation struct Compass: View { @StateObject var location = LocationManager() @State private var angle: CGFloat = 0 var body: some View { VStack { Image("arrow") .resizable() .aspectRatio(contentMode: .fit) .frame(width: 300,