combine

Swift Combine: Check if Subject has observer?

不羁的心 提交于 2020-01-24 23:09:05
问题 In RxSwift we can check if a *Subject has any observer, using hasObserver, how can I do this in Combine on e.g. a PassthroughSubject ? 回答1: No one time needed this... Apple does not provide this by API, and, actually, I do not recommend such thing, because it is like manually checking value of retainCount in pre-ARC Objective-C for some decision in code. Anyway it is possible. Let's consider it as a lab exercise. Hope someone find this helpful. Disclaimer: below code was not tested with all

ObservedObject inside ObservableObject not refreshing View

我与影子孤独终老i 提交于 2020-01-24 14:17:26
问题 I'm trying to display an activity indicator when performing an async request. What I did is creating an ActivityTracker object that will track life cycle of a publisher. This ActivityTracker is an ObservableObject and will be stored in the view model which also is an ObservableObject. It seems that this kind of setup isn't refreshing the View. Here's my code: struct ContentView: View { @ObservedObject var viewModel = ContentViewModel() var body: some View { VStack(spacing: 16) { Text("Counter

Swift. Combine. Is there any way to call a publisher block more than once when retry?

自古美人都是妖i 提交于 2020-01-24 05:35:05
问题 I want to make a network request more than one time when some error occurs using retry() from Swift/Combine. The block inside the publisher is called once only which means one only one request is made for a real app when error happens. My code is: import UIKit import Combine import PlaygroundSupport enum TestFailureCondition: Error { case invalidServerResponse } var backgroundQueue: DispatchQueue = DispatchQueue(label: "backgroundQueue") var failPublisher: AnyPublisher<(Data, URLResponse),

NSManagedObject changes do not trigger objectWillChange

假如想象 提交于 2020-01-22 10:34:07
问题 I have a Core Data model with an entity generated into class Task . I am trying to get the Combine publisher objectWillChange from the NSManagedObject to send (automatically, without manual work), but it won't. The task entity has a name attribute. let task = Task(context: container.viewContext) let taskSubscription = task.objectWillChange.sink(receiveValue: { _ in print("Task changed") }) task.name = "Foo" // WILL NOT trigger If I call send manually, the subscription will work: task

SwiftUI unable to navigate back and forth with navigationLink

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-16 10:32:12
问题 Notice in the gif that once I navigate and dismiss the new view, I am unable to navigate back! Is this a SwiftUI bug or a misuse of NavigationLinks? struct ContentView: View { var body: some View { return NavigationView { NavigationLink(destination: FakeView1()) { Text("Navigate") } } } } struct FakeView1: View { var body: some View { Text("Hey") } } 回答1: This seems swiftUI bug. I also faced the same issue so, I have used this workaround for it. struct ContentView: View { @State var

SwiftUI unable to navigate back and forth with navigationLink

时光总嘲笑我的痴心妄想 提交于 2020-01-16 10:31:50
问题 Notice in the gif that once I navigate and dismiss the new view, I am unable to navigate back! Is this a SwiftUI bug or a misuse of NavigationLinks? struct ContentView: View { var body: some View { return NavigationView { NavigationLink(destination: FakeView1()) { Text("Navigate") } } } } struct FakeView1: View { var body: some View { Text("Hey") } } 回答1: This seems swiftUI bug. I also faced the same issue so, I have used this workaround for it. struct ContentView: View { @State var

SwiftUI unable to navigate back and forth with navigationLink

坚强是说给别人听的谎言 提交于 2020-01-16 10:31:37
问题 Notice in the gif that once I navigate and dismiss the new view, I am unable to navigate back! Is this a SwiftUI bug or a misuse of NavigationLinks? struct ContentView: View { var body: some View { return NavigationView { NavigationLink(destination: FakeView1()) { Text("Navigate") } } } } struct FakeView1: View { var body: some View { Text("Hey") } } 回答1: This seems swiftUI bug. I also faced the same issue so, I have used this workaround for it. struct ContentView: View { @State var

SwiftUI Wrapper for UITextView not updating ObservedObject

不想你离开。 提交于 2020-01-15 04:59:13
问题 I hope I'm wrong, but I have not been able to find a SwiftUI equivalent to an editable UITextView. So, I built one using UIViewRepresentable. Populating both a SwiftUI Text and my own view with the ObservableObject works - but updates made in my view are not propagated to the ObservableObject. I must be missing something important with the Binding concept. Any guidance would be appreciated. import SwiftUI import Combine struct ContentView: View { @ObservedObject var myOText: MyOText var body:

How to “forward” a @Published value

混江龙づ霸主 提交于 2020-01-14 03:40:09
问题 I am very new to SwiftUI and Combine, and even though I have plenty of experience with Swift, and a bit with ReactiveKit, I am finding it hard to get some basic stuff to work. For example, I am trying to add an isLoggedIn property on my ViewModel, which should simply "forward" the UserManager class' isLoggedIn property. With ReactiveKit this is rather trivial but with SwiftUI/Combine I can't get is to work. The value is only set once, and then never updated again. class UserManager:

How does Swift ReferenceWritableKeyPath work with an Optional property?

旧时模样 提交于 2020-01-12 07:39:09
问题 Ground of Being : It will help, before reading, to know that you cannot assign a UIImage to an image view outlet's image property through the keypath \UIImageView.image . Here's the property: @IBOutlet weak var iv: UIImageView! Now, will this compile? let im = UIImage() let kp = \UIImageView.image self.iv[keyPath:kp] = im // error No! Value of optional type 'UIImage?' must be unwrapped to a value of type 'UIImage' Okay, now we're ready for the actual use case. What I'm actually trying to