RxSwift筆記七其他操作符

簡介

    git地址: https://github.com/ReactiveX/RxSwift參考資料:http://t.swift.gg/d/2-rxswiftReactiveX是通過可觀察的流實現異步編程的一種API,它結合了觀察者模式、迭代器模式和函數式編程的精華,RxSwift 是 ReactiveX 編程思想的一種實現。
復制代碼

subscribe

訂閱,是連接觀察者和被觀察者的操作符.

public func subscribe(on: (event: RxSwift.Event<Self.E>) -> Void) -> Disposable 就是說我們接收到的是事件,在這里一般通過 switch case 獲取對應結果。

let sequenceOfInts = PublishSubject<Int>()_ = sequenceOfInts.subscribe {print($0)
}sequenceOfInts.on(.Next(1))
sequenceOfInts.on(.Completed)
復制代碼

subscribeNext

指訂閱next事件.

public func subscribeNext(onNext: (Self.E) -> Void) -> Disposable 因為只有一種事件,這里的 API 傳入的就是事件中包含的具體的值了。

let sequenceOfInts = PublishSubject<Int>()_ = sequenceOfInts.subscribeNext {print($0)
}sequenceOfInts.on(.Next(1))
sequenceOfInts.on(.Completed)
復制代碼

subscribeCompleted / subscribeError

只訂閱完成/錯誤事件.

subscribeOnnextOnErrorOnCompleted

public func subscribe(onNext onNext: (Self.E -> Void)? = default, onError: (ErrorType -> Void)? = default, onCompleted: (() -> Void)? = default, onDisposed: (() -> Void)? = default) -> Disposable

takeUntil

當另一個序列開始發射值時,忽略原序列發射的值。

let originalSequence = PublishSubject<Int>()
let whenThisSendsNextWorldStops = PublishSubject<Int>()_ = originalSequence.takeUntil(whenThisSendsNextWorldStops).subscribe {print($0)
}originalSequence.on(.Next(1))
originalSequence.on(.Next(2))
originalSequence.on(.Next(3))whenThisSendsNextWorldStops.on(.Next(1))originalSequence.on(.Next(4))
復制代碼

takeWhile

跟filter類似,filter 和 takeWhile 什么時候更能清晰表達你的意思,就用哪個。

let sequence = PublishSubject<Int>()_ = sequence.takeWhile { $0 < 4 }.subscribe {print($0)
}sequence.on(.Next(1))
sequence.on(.Next(2))
sequence.on(.Next(3))
sequence.on(.Next(4))
sequence.on(.Next(5))
復制代碼

amb

處理多個序列,只會關心多個序列中最先發送數據的序列,其他序列會被忽略.

let intSequence1 = PublishSubject<Int>()
let intSequence2 = PublishSubject<Int>()
let intSequence3 = PublishSubject<Int>()let _ = [intSequence1, intSequence2, intSequence3].amb().subscribe {print($0)
}intSequence2.onNext(10) // intSequence2 最先發射
intSequence1.onNext(1)
intSequence3.onNext(100)
intSequence1.onNext(2)
intSequence3.onNext(200)
intSequence2.onNext(20)let intSequence1 = PublishSubject<Int>()
let intSequence2 = PublishSubject<Int>()let _ = intSequence1.amb(intSequence2).subscribe { // 只用于比較兩個序列print($0)
}intSequence2.onNext(10) // intSequence2 最先發射
intSequence1.onNext(1)
intSequence1.onNext(2)
intSequence2.onNext(20)復制代碼

concat

將兩個序列合并成一個序列,當一個序列中的所有元素成功發射完成之后,才會發射另一個序列中的元素。 在第一個序列發射完成之前,第二個序列發射的事件都會被忽略,但會接收第一個序列發射完成前第二個序列發射的最后一個事件。

如有侵權,請告知刪除.

轉載于:https://juejin.im/post/5ab1ee7a518825555c1d887a

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/278270.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/278270.shtml
英文地址,請注明出處:http://en.pswp.cn/news/278270.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

python學習

為了學會能學&#xff0c;不負時間&#xff0c;為了那簡練的美&#xff01; 為了一片新天地。 /t 對齊 python : # 99乘法表i 0while i < 9 : i 1 j 0 while j < i : j 1 print(j ,* , i,"" , i*j , end\t) #空格不能對齊 制表符…

hey 安裝_如何在助理揚聲器上調整“ Hey Google”的靈敏度

hey 安裝Vantage_DS/ShutterstockVantage_DS / ShutterstockThe Google Assistant is a useful tool that allows you to control your smart home, check the weather, and more. Unfortunately, the Assistant might not hear you in a noisy environment or it might activa…

EXCEL如何進行多條件的數據查找返回

在使用EXCEL時經常會碰到一個表里的同一款產品每天的銷量都不一樣&#xff0c;然后我們需要查導出每一款產品每天的銷量&#xff0c;即一對多條件查找。這個教復雜&#xff0c;我們要用到好幾個函數的綜合&#xff0c;下面小編來教你吧。 工具/原料 EXCEL軟件&#xff08;本文使…

如何將Google幻燈片轉換為PowerPoint

If someone sent you a presentation on Google Slides, but you’d rather work on it in Microsoft PowerPoint, you can easily convert it to a .pptx file in just a few simple steps. Here’s how it’s done. 如果有人在Google幻燈片上向您發送了演示文稿&#xff0c;但…

Linux 常用命令:文本查看篇

前言 Linux常用命令中&#xff0c;除了cat還有很多其他用于文本查看的命令。本文將簡單介紹一下這些文本查看的命令。 全文本顯示--cat cat可能是常用的一個文本查看命令了&#xff0c;使用方法也很簡單&#xff1a; cat file #全文本顯示在終端cat -n file #顯示全文本&#…

XP調整禁用頁面文件

NOTE: You shouldn’t disable your page file unless you really really know what you are doing. 注意&#xff1a;除非您真的很清楚自己在做什么&#xff0c;否則不應該禁用頁面文件。 If your computer has 1 GB of RAM or more, disabling the page file can speed up XP…

復制數據庫

1.首先手動創建新的數據庫 CREATE DATABASE new-DB DEFAULT CHARACTER SET utf8mb4; 2.使用mysqldump命令。結果&#xff0c;會連同數據一起復制過去。 mysqldump old-DB -u root -pXX --add-drop-table | mysql new-DB -u root -pXX轉載于:https://www.cnblogs.com/clcliangc…

labelme2coco問題:TypeError: Object of type 'int64' is not JSON serializable

最近在做MaskRCNN 在自己的數據&#xff08;labelme&#xff09;轉為COCOjson格式遇到問題&#xff1a;TypeError: Object of type int64 is not JSON serializable 原因是numpy的數據類型不能被json兼容 最簡單的做法是自己寫一個序列類 class MyEncoder(json.JSONEncoder):de…

如何在Windows 10的命令提示符中更改目錄

One of the first things you’ll need to learn as you become more familiar with Command Prompt on Windows 10 is how to change directories in the operating system’s file system. There are a few ways you can do this, so we’ll walk you through them. 隨著您對…

jQuery 購物車

html代碼 <!--shoppingCar start--> <table id"TB"> <tr> <td colspan"7" class"title"> <div class"img_box"> <div class"logo_box"> <img src"img/jd…

MySQL優化總結

2019獨角獸企業重金招聘Python工程師標準>>> 從這幾天看MySQL性能優化來看&#xff0c;基本的思路就是分分分&#xff0e;&#xff0e;&#xff0e; 1&#xff0c;分讀&#xff0c;用緩存來分攤讀表的壓力; 2&#xff0c;讀寫分離&#xff0c;主從分離&#xff0c;寫…

瘋狂java講義

封裝&#xff1a; 封裝&#xff1a;將對象狀態信息隱藏在對象內部&#xff0c;不允許外部程序直接訪問對象內部信息&#xff0c;而是用類所提供的方法訪問和操作。訪問控制符&#xff1a;private&#xff08;當前類訪問權&#xff09; ,protected&#xff08;子類訪問&#xff…

將背景色添加到Word 2007文檔中

Instead of using the standard white background with Word documents, here is how to add some background color to spice up your documents. 代替在Word文檔中使用標準的白色背景&#xff0c;這是如何添加一些背景顏色來為文檔增添色彩。 Open your word document and ch…

jquery實現增刪改(偽)-老男孩作業day13

使用jquery進行&#xff0c;文件的編寫&#xff0c;實現自增id,刪除&#xff0c;添加&#xff0c;編輯模式。 jquery放在本地&#xff0c;src"jquery_js.js" 可以改成其他&#xff0c;或者在線的路徑 readme<!DOCTYPE html> <html lang"en"> &…

uoj#119. 【UR #8】決戰圓錐曲線(線段樹+復雜度分析)

題解 傳送門 題解 然而要我來說我感覺只是個爆搜啊…… //minamoto #include<bits/stdc.h> #define R register #define ll long long #define ls (p<<1) #define rs (p<<1|1) #define fp(i,a,b) for(R int ia,Ib1;i<I;i) #define fd(i,a,b) for(R int ia…

如何在Raspberry Pi上設置兩因素身份驗證

Kiklas/ShutterstockKiklas /快門The Raspberry Pi is everywhere now, which is why it’s caught the eye of threat actors and cybercriminals. We’ll show you how to secure your Pi with two-factor authentication. Raspberry Pi現在無處不在&#xff0c;這就是為什么…

Laravel Passport里的授權類型介紹

本文來自pilishen.com----原文鏈接; 歡迎來和pilishen一起學習php&Laravel&#xff1b;學習群&#xff1a;109256050OAuth2是一個安全框架&#xff0c;控制著程序受保護部分的準入&#xff0c;主要是控制不同的客戶端如何來調取API&#xff0c;保證它們在請求相應資源的時候…

vue 開發環境搭建

1.創建vue項目 1.node js 生成項目&#xff0c;編譯項目 2.hbuilder 開發環境 1.下載安裝node js http://nodejs.cn/download/ 確認是否安裝成功 如果安裝不了 代碼錯誤2503 解決方法&#xff1a; 管理員命令運行cmd; cd\ cd C:\Users\Administrator\Desktop msiexec/package n…

iphone視圖放大_如何將iPhone用作放大鏡

iphone視圖放大It’s a common problem: Some things are just too hard to see. Usually, they’re too far away, too dark, or too small. With a feature called Magnifier, your iPhone can function as a magnifying glass and sight aid. Here’s how to use it. 這是一…

chmod權限

本文引用:https://www.cnblogs.com/zhangym/p/5826200.html linux數字權限解釋 644 第一位代表文件所有者的權限,第二位代表同組用戶的權限&#xff0c;第三位代表其他用戶的權限. 讀取權限等于4,用r表示;寫入權限2,用w表示;執行權限1,用x表示. 通過4、2、1組合,得到以下幾種權…