繁体   English   中英

RxSwift + 用户默认值

[英]RxSwift + UserDefaults

我是 RxSwift 的新手,就我而言,我想使用 UserDefaults 和 RxSwift 来简化我的代码,所以我做了以下代码

我的问题是,当我单击一个单元格时,但订阅方法提交了两次? 那么我该怎么做才能解决它? 多谢!

import UIKit

import RxSwift
import RxCocoa
import RxDataSources

class ViewController: UIViewController {
    let disposeBag = DisposeBag()

    @IBOutlet weak var tableView: UITableView! {
        didSet {
            tableView.register(UITableViewCell.self, forCellReuseIdentifier: String(describing: UITableViewCell.self))

            tableView.rx
                .itemSelected
                .subscribe { (indexPath) in
                    UserDefaults.standard.set("\(indexPath)", forKey: "key")
                }
                .disposed(by: disposeBag)
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        UserDefaults.standard.rx
            .observe(String.self, "key")
            // .debug()
            .subscribe(onNext: { (value) in
                if let value = value {
                    print(value)
                }
            })
            .disposed(by: disposeBag)

        let dataSource = RxTableViewSectionedReloadDataSource<SectionModel<String, String>>()

        dataSource.configureCell = { (dataSource, tableView, indexPath, item) in
            let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: UITableViewCell.self), for: indexPath)
            cell.textLabel?.text = item

            return cell
        }

        Observable.just([SectionModel(model: "", items: (0..<5).map({ "\($0)" }))])
            .bindTo(tableView.rx.items(dataSource: dataSource))
            .disposed(by: disposeBag)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

这确实是某种错误,我建议使用distinctUntilChanged()

使用@wisper 建议的 debounce debounce()可能在大部分时间都有效,但很危险,因为您依赖于 observable 发出的事件的速度。

iOS 错误,v10.2

UserDefaults.standard.rx
    .observe(String.self, "key")
+   .debounce(0.1, scheduler: MainScheduler.asyncInstance)
    ...

您可以尝试使用 take(n) ,其中 'n' 是可观察序列中连续元素的数量。

tableView.rx
  .itemSelected
  .take(1)
  .subscribe { (indexPath) in
    UserDefaults.standard.set("\(indexPath)", forKey: "key")
  }
  .disposed(by: disposeBag)

如果您只想确保它在您的场景中只发出一次,请按照其他人的建议使用distinctUntilChanged() 请注意,如果您点击同一个单元格两次,您的subscribe闭包只会发出一次。

如果你想更好地理解为什么它发出两次,我会检查tableView上的didSet是否被调用了两次。 你可以尝试移动这个块

tableView.rx
  .itemSelected
  .subscribe { (indexPath) in
    UserDefaults.standard.set("\(indexPath)", forKey: "key")
}
.disposed(by: disposeBag)

viewDidLoad()看看你是否有同样的问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM