简体   繁体   English

RxSwift + 用户默认值

[英]RxSwift + UserDefaults

I am new here about RxSwift, In my case, I want to use UserDefaults with RxSwift to simplify my code, so I did this following code我是 RxSwift 的新手,就我而言,我想使用 UserDefaults 和 RxSwift 来简化我的代码,所以我做了以下代码

my question is, when I clicked a cell, but the subscribe method submit twice?我的问题是,当我单击一个单元格时,但订阅方法提交了两次? so what should I do to fix it?那么我该怎么做才能解决它? thx a lot!多谢!

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.
    }
}

It's indeed some kind of a bug and I would recommed to use distinctUntilChanged() .这确实是某种错误,我建议使用distinctUntilChanged()

Using debounce() as suggested by @wisper might work most of the time but is dangerous because you are relying on speed of events emitted by observable.使用@wisper 建议的 debounce debounce()可能在大部分时间都有效,但很危险,因为您依赖于 observable 发出的事件的速度。

iOS bug, v10.2 iOS 错误,v10.2

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

You can try using take(n) where 'n' is the number of contiguous elements from an observable sequence.您可以尝试使用 take(n) ,其中 'n' 是可观察序列中连续元素的数量。

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

If you just want to make sure it emits only once in your scenario, use distinctUntilChanged() as others have suggested.如果您只想确保它在您的场景中只发出一次,请按照其他人的建议使用distinctUntilChanged() Note that if you tap on the same cell twice, your subscribe closure will only emit once.请注意,如果您点击同一个单元格两次,您的subscribe闭包只会发出一次。

If you want to get a better understanding of why it is emitting twice, I would check if the didSet on tableView is called twice.如果你想更好地理解为什么它发出两次,我会检查tableView上的didSet是否被调用了两次。 You could try moving this block你可以尝试移动这个块

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

to viewDidLoad() and see if you have the same problem.viewDidLoad()看看你是否有同样的问题。

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

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