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

Usage with SwiftUI #849

Open
learnwithgabbar opened this issue May 4, 2022 · 4 comments
Open

Usage with SwiftUI #849

learnwithgabbar opened this issue May 4, 2022 · 4 comments

Comments

@learnwithgabbar
Copy link

No description provided.

@mluisbrown
Copy link
Contributor

Can you describe your issue? ReactiveSwift works fine with SwiftUI, although you will need to use Combine to bridge state changes to an ObservableObject which is what SwiftUI uses for observing state changes and updating the View.

@learnwithgabbar
Copy link
Author

I want to map property with @published, any example will help a lot

@learnwithgabbar
Copy link
Author

Can you describe your issue? ReactiveSwift works fine with SwiftUI, although you will need to use Combine to bridge state changes to an ObservableObject which is what SwiftUI uses for observing state changes and updating the View.

How

@mluisbrown
Copy link
Contributor

You will need to create an ObservableObject wrapper around the Propertys in your model and when they change, call the send method on the ObservableObjectPublisher.

For example, something like this:

import Combine
import SwiftUI
import ReactiveSwift

class ViewModel: ObservableObject {
  public private(set) lazy var objectWillChange = ObservableObjectPublisher()
  public let count = MutableProperty(0)

  init() {
    count.producer.startWithValues { [weak self] _ in
      self?.objectWillChange.send()
    }
  }
}


struct CounterView: View {
  @ObservedObject var viewModel: ViewModel

  init(viewModel: ViewModel) {
    self.viewModel = viewModel
  }

  var body: some View {
    Text("Count is \(viewModel.count.value)")
  }
}

There is no direct way to map Property to ObservableObject or Published.

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