You don't give a lot of detail, despite the code you posted. I will address your specific comment, "What I wanted to ask you was to enter the list screen and get the data. Then it went into the detail screen and changed specific data. And if it go to the list screen, it have to update the data."
Using my CLE library (which you can install with cocoapods or SPM) doing this is quite simple.
let change = changeButton.rx.tap
.flatMapFirst(presentScene(animated: true) {
DetailViewController.scene { $0.connect() }
})
Here is a complete example that you can run yourself to see how it works. To run the below, just add XIB files for the two view controllers, and hook up the outlets.
import Cause_Logic_Effect
import RxCocoa
import RxSwift
final class MainViewController: UIViewController {
@IBOutlet var addButton: UIButton!
@IBOutlet var tableView: UITableView!
let disposeBag = DisposeBag()
}
final class DetailViewController: UIViewController {
@IBOutlet var saveButton: UIButton!
@IBOutlet var nameField: UITextField!
let disposeBag = DisposeBag()
}
extension MainViewController {
func connect() {
let initial = Observable<[String]>.just([]) // This could be a network request
let addName = addButton.rx.tap
.flatMapFirst(presentScene(animated: true) {
DetailViewController().scene { $0.connect() }
})
let state = mainVieweModel(initial: initial, addName: addName)
.share(replay: 1)
state
.bind(to: tableView.rx.items(cellIdentifier: "Cell", cellType: UITableViewCell.self)) { _, name, cell in
cell.textLabel?.text = name
}
.disposed(by: disposeBag)
}
}
func mainVieweModel(initial: Observable<[String]>, addName: Observable<String>) -> Observable<[String]> {
enum Input {
case initial([String])
case add(String)
}
return Observable.merge(
initial.map { Input.initial($0) },
addName.map { Input.add($0) }
)
.scan(into: [String]()) { state, input in
switch input {
case let .initial(value):
state = value
case let .add(text):
state.append(text)
}
}
}
extension DetailViewController {
func connect() -> Observable<String> {
return saveButton.rx.tap
.withLatestFrom(nameField.rx.text.orEmpty)
.take(1)
}
}
The app delegate looks like this:
import Cause_Logic_Effect
import UIKit
@main
final class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
window = {
let result = UIWindow(frame: UIScreen.main.bounds)
result.rootViewController = MainViewController().configure { $0.connect() }
result.makeKeyAndVisible()
return result
}()
return true
}
}