Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: about your article of stranger-things-swiftui-state #43

Open
dochoi-bot opened this issue Nov 29, 2020 · 2 comments
Open

Question: about your article of stranger-things-swiftui-state #43

dochoi-bot opened this issue Nov 29, 2020 · 2 comments

Comments

@dochoi-bot
Copy link

dochoi-bot commented Nov 29, 2020

Hi
First of all, thank you
I'm learning a lot about clean architecture for Swift UI from you

stranger-things-swiftui-state in the "Clean Architecture for SwiftUI" article, "Coordinator is history" Section
In this scenario,

struct TestView: View {

    let publisher = PassthroughSubject<String, Never>()    // 1
    let publisher = CurrentValueSubject<String, Never>("") // 2
    
    var body: some View {
        MyView(publisher: publisher.eraseToAnyPublisher())
    }
}

Scenario 2 works differently.

Here's my code.

import SwiftUI

@main
struct TestSwiftUIApp: App {
    
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}
import SwiftUI
import Combine

struct ContentView: View {
    let publisher = CurrentValueSubject<String, Never>("") // 2
    var body: some View {
        MyView(publisher: publisher.eraseToAnyPublisher())
    }
    
}

struct MyView: View {
    
    let publisher: AnyPublisher<String, Never>
    
    @State var text: String = ""
    @State var didAppear: Bool = false
    
    var body: some View {
        Text(text)
            .onAppear { self.didAppear = true }
            .onReceive(publisher) {
                print("onReceive")
                self.text = $0
            }
    }
}

image

my answer is 1...

also

struct MyView: View {
    
    let publisher: AnyPublisher<String, Never>
    
    @State var text: String = ""
    @State var didAppear: Bool = false
    
    var body: some View {
        Text(text)
            .onAppear { self.didAppear = true
                self.text = "abc"
            }
            .onReceive(publisher) {
                print("onReceive")
                self.text = $0
            }
    }
}

my answer is "abc"...

image

The result is different because of SwifttUI 2.0? or I'm missing something?

Thanks

@nalexn
Copy link
Owner

nalexn commented Nov 29, 2020

Possibly a SwiftUI 2.0 change. Could you try running this on iOS 13?

@dochoi-bot
Copy link
Author

I just installed the 13.0 simulator. and built.
Here's my code

import SwiftUI
import Combine

struct ContentView: View {
    let publisher = CurrentValueSubject<String, Never>("") // 2
    var body: some View {
        MyView(publisher: publisher.eraseToAnyPublisher())
    }
    
}

struct MyView: View {
    
    let publisher: AnyPublisher<String, Never>
    
    @State var text: String = ""
    @State var didAppear: Bool = false
    
    var body: some View {
        Text(text)
            .onAppear { self.didAppear = true
                self.text = "abc"
                print(UIDevice.current.systemVersion)
            }
            .onReceive(publisher) {
                print("onReceive")
                self.text = $0
            }
    }
}

image

Something seems to be updated

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants