現在、APIリクエストにはRxを使用しています。使用方法の例は次のとおりです。
let orderRxService = OrderRxService.listAsShop(shopId, status: .active)
.repeatRequest(delay: 4)
.observeOn(MainScheduler.instance)
.subscribe( onNext: { [weak self] orders in
self?.orders = orders
self?.tableView.reloadData()
})
.disposed(by: disposeBag)
これshopId
により、ステータスが指定されたすべての注文が取得されます.active
。更新のたびに、ローカルorders
オブジェクトが置き換えられ、tableViewが再ロードされます。
これにより、tableView全体がリロードされますが、これは避けたいものです。私は現在RxDataSourcesを調べていますが、これを機能させる方法を実際に理解することはできません。
Order
オブジェクトは、別のプロパティ有するcurrentStatus
3つの異なる値とすることができます。3つの異なるセクションを持つtableViewがあり、各セクションにはのすべての注文が表示されますcurrentStatus
。
これはRxDataSourcesでどのように実装する必要がありますか?理想的には、前に示したサービスにバインドすることOrderRxService.....subscribe()..
です()。
RxDataSources-typesを設定するために今持っているのは次のとおりです。
extension Order: IdentifiableType, Equatable {
public typealias Identity = String
public var identity: String {
return String(id)
}
public static func == (lhs: Order, rhs: Order) -> Bool {
return (lhs.timeCreated ?? 0) > (rhs.timeCreated ?? 0)
}
}
struct OrdersSection {
var header: String
var orders: [Order]
}
extension OrdersSection: AnimatableSectionModelType {
typealias Item = Order
typealias Identity = String
var identity: String {
return header
}
var items: [Item] {
set {
orders = items
}
get {
return orders
}
}
init(original: OrdersSection, items: [Order]) {
self = original
self.items = items
}
}
私がそれを機能させようとしたのは:
// I tried to make our local orders a Variable (I don't like this between-step and would like this to be just [Order]).
var orders: Variable<[Order]> = Variable([])
fun viewDidLoad() {
super.viewDidLoad()
// Then I set the local orders-variable's value to the new value coming from our Rx service.
let orderRxDisposable: Disposable = OrderRxService.listAsShop(shopId, status: .active)
.repeatRequest(delay: 4)
.observeOn(MainScheduler.instance)
.map { $0.items }.subscribe( onNext: { [weak self] orders in self?.orders.value = orders }) // Here I setup the dataSource let dataSource = RxTableViewSectionedAnimatedDataSource<OrdersSection>( configureCell: { ds, tv, ip, item in let cell = tv.dequeueReusableCell(withIdentifier: "OrderCell", for: ip) as! OrderCell cell.addContent(item, tableView: tv, viewController: self, spotDelegate: self) return cell }, titleForHeaderInSection: { ds, ip in return ds.sectionModels[ip].header } ) // Here I set up the three different sections. self.orders.asObservable().observeOn(MainScheduler.instance) .map { o in o.filter { $0.currentStatus == .status_one }
}
.map { [OrdersSection(header: "Status one", orders: $0)] } .bind(to: self.tableView.rx.items(dataSource: dataSource)) self.orders.asObservable().observeOn(MainScheduler.instance) .map { o in o.filter { $0.currentStatus == .status_two }
}
.map { [OrdersSection(header: "Status two", orders: $0)] } .bind(to: self.tableView.rx.items(dataSource: dataSource)) self.orders.asObservable().observeOn(MainScheduler.instance) .map { o in o.filter { $0.currentStatus == .status_three }
}
.map { [OrdersSection(header: "Status three", orders: $0)] }
.bind(to: self.tableView.rx.items(dataSource: dataSource))
}
おそらく改善できるさまざまな側面があります。たとえば、Variable<[Order]>
私はただになりたい[Order]
です。そして、これを監視可能にする代わりに、それを完全にスキップして、OrderRxServiceを監視することで3つの異なるセクションを作成できますか?
次のようなものにすることは可能でしょうか?
OrderRxService.listAsshop(shopId, status: .active).observeOn(MainScheduler.instance)
// First section
.map { o in
o.filter { $0.status == .status_one }
}
.map { [OrdersSection(header: "Status one", orders: $0)] } .bind(to: self.tableView.rx.items(dataSource: dataSource)) // Second section .map { o in o.filter { $0.status == .status_two }
}
.map { [OrdersSection(header: "Status two", orders: $0)] }
.bind(to: self.tableView.rx.items(dataSource: dataSource))
// Etc...
助けてくれてありがとう!
次のようなモデルを作成できます。
enum SectionModel {
case SectionOne(items: [SectionItem])
case SectionTwo(items: [SectionItem])
case SectionThree(items: [SectionItem])
}
enum SectionItem {
case StatusOne()
case StatusTwo()
case StatusThree()
}
extension SectionModel: SectionModelType {
typealias Item = SectionItem
var items: [SectionItem] {
switch self {
case .SectionOne(items: let items):
return items.map { $0 } case .SectionTwo(items: let items): return items.map { $0 }
case.SectionThree(items: let items):
return items.map { $0 }
}
}
init(original: SectionModel, items: [Item]) {
switch original {
case .SectionOne(items: _):
self = .SectionOne(items: items)
case .SectionTwo(items: _):
self = .SectionTwo(items: items)
case .SectionThree(items: _):
self = .SectionThree(items: items)
}
}
}
データソース内のさまざまなアイテムを処理します
dataSource = RxCollectionViewSectionedReloadDataSource<SectionModel>(configureCell: { (datasource, collectionView, indexPath, _) in
switch datasource[indexPath] {
case .StatusOne:
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: R.reuseIdentifier.statusCellOne, for: indexPath)!
// do stuff
return cell
case .StatusTwo:
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: R.reuseIdentifier.statusCellTwo, for: indexPath)!
// do stuff
return cell
case .StatusThree:
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: R.reuseIdentifier.statusCellThree, for: indexPath)!
// do stuff
return cell
}
})
次に、odersをSectionItem
forにマップSectionModel
し、dataSourceにバインドします
Reba McEntire が息子の Shelby Blackstock と共有しているクリスマスの伝統について学びましょう。
メーガン・マークルとマライア・キャリーが自然な髪の上でどのように結合したかについて、メーガンの「アーキタイプ」ポッドキャストのエピソードで学びましょう.
ハリー王子が家族、特にチャールズ王とウィリアム王子との関係について望んでいると主張したある情報源を発見してください。
ワイノナ・ジャッドが、母親のナオミ・ジャッドが亡くなってから初めての感謝祭のお祝いを主催しているときに、彼女が今では家長であることをどのように認識したかを学びましょう.
Air travel is far more than getting from point A to point B safely. How much do you know about the million little details that go into flying on airplanes?
The world is a huge place, yet some GeoGuessr players know locations in mere seconds. Are you one of GeoGuessr's gifted elite? Take our quiz to find out!
ドナルドグローバーの「ディスイズアメリカ」は、それがビデオ自体(5月6日にデビューしてから1億900万回以上視聴された)であろうと、ビデオに関する公の言説(ひいてはグローバー、別名)であろうと、先週避けられませんでした。幼稚なガンビーノ、彼自身)。現在、「ディス・イズ・アメリカ」はNo.でデビューします。
Trailer Happy Hourにようこそ。オーディオビジュアルのヴァルハラでは、すべての優れた映画のプロモーションが、あなたの愛、注目、クリックのために永遠に戦います。今日は、ワイルドパーティー、警察が関与する銃撃、80年代の漫画の雑学クイズを打ち破る怒り狂ったウィルアーネットがいるので、すぐに飛び込みましょう。
(写真:パトリック・スミス/ゲッティイメージズ)削除されたInstagramの投稿で、女優のヘザー・リンドがジョージHWを主張しました
ロシアのフィギュアスケーター、カミラ・バリエバが関与したドーピング事件が整理されているため、チームは2022年北京冬季オリンピックで獲得したメダルを待っています。
何千人ものAmazonの買い物客がMulberry Silk Pillowcaseを推奨しており、現在販売中. シルクの枕カバーにはいくつかの色があり、髪を柔らかく肌を透明に保ちます。Amazonで最大46%オフになっている間にシルクの枕カバーを購入してください
ラファイエット警察署は、「不審な男性が女性に近づいた」という複数の苦情を受けて、12 月にパデュー大学の教授の捜査を開始しました。
私たちの周りの世界と同じように、言語は常に変化しています。以前の時代では、言語の変化は数年または数十年にわたって発生していましたが、現在では数日または数時間で変化する可能性があります。
認知症を患っている 91 歳のアジア人女性が最近、47 番街のアウター サンセット地区でロメオ ロレンゾ パーハムに襲われました。伝えられるところによると、被害者はサンフランシスコの通りを歩いていたところ、容疑者に近づき、攻撃を受け、暴行を受けました。
Cómo mejoramos la accesibilidad de nuestro componente de precio, y cómo nos marcó el camino hacia nuevos saberes para nuestro sistema de diseño. Por Ana Calderon y Laura Sarmiento Leer esta historia en inglés.
“And a river went out of Eden to water the garden, and from thence it was parted and became into four heads” Genesis 2:10. ? The heart is located in the middle of the thoracic cavity, pointing eastward.