combine

How do I resume a published timer in SwiftUI after navigating to a different page?

主宰稳场 提交于 2020-08-20 11:26:45
问题 In the following Playground example, the UI updates with the current date correctly, however when navigating away from the page and coming back the timer does not resume ticking: import SwiftUI import PlaygroundSupport struct MainTabView: View { let timer = Timer.publish(every: 1, on: .main, in: .common) @State var time = Date() var body: some View { TabView { VStack { Text("\(time)").onReceive(self.timer) { self.time = $0 } } .onAppear { _ = self.timer.connect() } .tabItem { Text("Page 1") }

How to solve deadlock on multiple Published on 2 TextField in SwiftUI?

孤人 提交于 2020-08-10 19:14:13
问题 I have two Published state in my viewmodel one is for source and another one is for destination Based on the solution here to detect live changes on textfield. I have implemented like this TextField("", text: $viewModel.source) TextField("", text: $viewModel.destination) Problem, here is i need to update the either fields whenever user enter some value. For example - If user enter on source then i need to update destination with source inputs. And if user input destination then need to update

How to solve deadlock on multiple Published on 2 TextField in SwiftUI?

耗尽温柔 提交于 2020-08-10 19:13:09
问题 I have two Published state in my viewmodel one is for source and another one is for destination Based on the solution here to detect live changes on textfield. I have implemented like this TextField("", text: $viewModel.source) TextField("", text: $viewModel.destination) Problem, here is i need to update the either fields whenever user enter some value. For example - If user enter on source then i need to update destination with source inputs. And if user input destination then need to update

Published computed properties in SwiftUI model objects

五迷三道 提交于 2020-08-05 06:33:29
问题 Suppose I have a data model in my SwiftUI app that looks like the following: class Tallies: Identifiable, ObservableObject { let id = UUID() @Published var count = 0 } class GroupOfTallies: Identifiable, ObservableObject { let id = UUID() @Published var elements: [Tallies] = [] } I want to add a computed property to GroupOfTallies that resembles the following: // Returns the sum of counts of all elements in the group var cumulativeCount: Int { return elements.reduce(0) { $0 + $1.count } }

Combine framework: how to process each element of array asynchronously before proceeding

假装没事ソ 提交于 2020-08-01 09:58:26
问题 I'm having a bit of a mental block using the iOS Combine framework. I'm converting some code from "manual" fetching from a remote API to using Combine. Basically, the API is SQL and REST (in actual fact it's Salesforce, but that's irrelevant to the question). What the code used to do is call a REST query method that takes a completion handler. What I'm doing is replacing this everywhere with a Combine Future. So far, so good. The problem arises when the following scenario happens (and it

An equivalent to computed properties using @Published in Swift Combine?

给你一囗甜甜゛ 提交于 2020-08-01 05:32:06
问题 In imperative Swift, it is common to use computed properties to provide convenient access to data without duplicating state. Let's say I have this class made for imperative MVC use: class ImperativeUserManager { private(set) var currentUser: User? { didSet { if oldValue != currentUser { NotificationCenter.default.post(name: NSNotification.Name("userStateDidChange"), object: nil) // Observers that receive this notification might then check either currentUser or userIsLoggedIn for the latest

ObservedObject view-model is still in memory after the view is dismissed

拟墨画扇 提交于 2020-07-30 07:54:31
问题 I am having some trouble with memory management in SwiftUI and Combine. For example, if I have a NavigationView and then navigate to a detail view with a TextField, and enter a value in the TextField and tap on the back button, next time I go to that view the TextField has the previously entered value. I noticed that the view-model is still in memory after the detail view is dismissed, and that's probably why the TextField still holds a value. In UIKit, when dismissing a ViewController, the