{"id":4431,"date":"2022-02-16T23:23:47","date_gmt":"2022-02-16T15:23:47","guid":{"rendered":"https:\/\/1989lifejourney.com\/?p=4431"},"modified":"2022-04-23T15:05:19","modified_gmt":"2022-04-23T07:05:19","slug":"rxswift%e7%b0%a1%e4%bb%8b","status":"publish","type":"post","link":"https:\/\/1989lifejourney.com\/?p=4431","title":{"rendered":"Getting started with RxSwift"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\" id=\"\u4ee5\u4e0b\u8cc7\u6599\u53c3\u8003\u81earef-\u9019\u88e1\u50c5\u4f5c\u500b\u4eba\u5b78\u7fd2\u6574\u7406\"><strong>\u4ee5\u4e0b\u8cc7\u6599\u53c3\u8003\u81ea<a href=\"https:\/\/ithelp.ithome.com.tw\/articles\/10237162\">Ref<\/a>\uff0c\u9019\u88e1\u50c5\u4f5c\u500b\u4eba\u5b78\u7fd2\u6574\u7406<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<ul class=\"has-medium-font-size wp-block-list\"><li><strong>Definition of ReactiveX<\/strong><\/li><li><strong>Advantage of ReactiveX<\/strong><\/li><li><strong>Definition of RxSwift<\/strong><\/li><li><strong>Example of RxSwift<\/strong><\/li><li><strong>\u88dc\u5145<\/strong><\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"reactivex\"><strong><strong>Definition of ReactiveX<\/strong><\/strong>:<\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li>An API for asynchronous programming with observable streams.<\/li><li>ReactiveX is a combination of the best ideas from the <strong>Observer pattern<\/strong>, the <strong>Iterator pattern<\/strong>, and <strong>functional programming<\/strong>.<\/li><li>ReactiveX\u662f\u4e00\u7d44tool\u8b93\u6307\u4ee4\u5f0f\u7a0b\u5f0f\u8a2d\u8a08(imperative programming)\u7684\u7a0b\u5f0f\u8a9e\u8a00\u80fd\u5920\u4e0d\u7528\u7ba1\u8cc7\u6599\u662f\u540c\u6b65\u6216\u975e\u540c\u6b65\u7684\u72c0\u6cc1\u4e0b\u53bb\u64cd\u4f5c\u8cc7\u6599\u5e8f\u5217\u3002\u5b83\u63d0\u4f9b\u4e86\u4e00\u7d44sequence operators\u4f86\u64cd\u4f5c\u5728\u5e8f\u5217\u4e0a\u7684\u6bcf\u500bitem\u3002<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u7528\u4e00\u53e5\u8a71\u8aaa\u660eRx:<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-pullquote\"><blockquote><p><em style=\"color:inherit;font-family:-apple-system, BlinkMacSystemFont, &quot;\">\u5229\u7528observer pattern\u548csequence operators\uff0c\u8b93\u6211\u5011\u80fd\u5920\u7528\u5ba3\u544a\u5f0f(declarative)\u7684\u65b9\u5f0f\u53bb\u8655\u7406\u540c\u6b65\u6216\u975e\u540c\u6b65\u7684data or event stream\u3002<\/em><\/p><\/blockquote><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Advantage of Rx:<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li>Composable, Reusable <strong>(p.s. \u8a02\u95b1\u8005\u6a21\u5f0f\u512a\u9ede)<\/strong><\/li><li>Understandable and concise <strong>(p.s. Declartive\u512a\u9ede)<\/strong><\/li><li>\u907f\u514dMutable state\uff0c\u8b93\u8cc7\u6599\u6d41\u5448\u55ae\u5411\u6d41\u52d5 <strong>(p.s. Function Reactive programming\u512a\u9ede)<\/strong><\/li><li>\u65b9\u4fbf\u8655\u7406\u975e\u540c\u6b65\u4e8b\u4ef6<strong>(p.s. Reactive programming\u7684\u512a\u9ede)<\/strong><\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"rxswift\"><strong>Definition of RxSwift<\/strong><\/h3>\n\n\n\n<p class=\"has-text-align-left has-medium-font-size wp-block-paragraph\">RxSwift is the Swift-specific implementation of the Reactive Extensions standard.<\/p>\n\n\n\n<p class=\"has-medium-font-size wp-block-paragraph\"><strong>Note:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\" style=\"font-size:18px;\">\u6709\u4eba\u8aaaRx\u662fFunctional Reactive Programing\uff08FRP\uff09\uff0c\u5728\u5b98\u65b9\u4ecb\u7d39\u4e2d\u6709\u7279\u5225\u8aaa\u660e\uff0c\u9019\u4e0d\u662f\u90a3\u9ebc\u6b63\u78ba\uff0cRx\u53ef\u80fd\u6703\u662fFunctional\uff0c\u53ef\u80fd\u6703\u662fReactive\uff0c\u4f46FRP\u662f\u53e6\u5916\u4e00\u4ef6\u6771\u897f\uff0c<strong>\u5728\u5b9a\u7fa9\u7684\u4e0a FRP \u5247\u662f\u96a8\u8457\u6642\u9593continuously\uff08\u9023\u7e8c\u7684\uff09\u64cd\u4f5c\u6578\u503c\uff0cRx\u5247\u662f discrete\uff08\u96e2\u6563\u7684\uff09<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Example of RxSwift<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6bcf0.5\u79d2\u8f38\u51fa\u4e00\u500b\u7d2f\u9032\u6578\u503c\uff0c\u85c9\u7531subscribe\uff0c\u89f8\u767c\u5230onNext\u9019\u500bclosure\uff0c\u5370\u51fa\u6536\u5230\u7684\u6578\u503c<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>       let observable = Observable&lt;Int&gt;.interval(.milliseconds(500), scheduler: MainScheduler.instance)\n        observable\n            .subscribe(onNext: { element in\n                print(element)\n            }, onError: { error in\n                print(error)\n            }, onCompleted: {\n                print(\"onCompleted\")\n            }, onDisposed: {\n                print(\"onDisposed\")\n            })<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u88dc\u5145:<\/h3>\n\n\n\n<ul class=\"wp-block-list\" id=\"block-1de94cc5-36b0-4414-b42f-af5b72b478bb\"><li><strong>Observer pattern(\u89c0\u5bdf\u8005\u6a21\u5f0f)<\/strong><\/li><li><strong>Iterator pattern(\u758a\u4ee3\u5668\u6a21\u5f0f)<\/strong><\/li><li><strong>Imperative programming\uff08\u6307\u4ee4\u5f0f\u7a0b\u5f0f\u8a2d\u8a08)<\/strong><\/li><li><strong>Declarative programming\uff08\u5ba3\u544a\u5f0f\u7a0b\u5f0f\u8a2d\u8a08)<\/strong><\/li><li><strong>Functional programming(\u51fd\u5f0f\u8a9e\u8a00\u7a0b\u5f0f\u8a2d\u8a08)<\/strong><\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Collections:<\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li>In Rx, you begin with a collection and end with a collection<\/li><li>Rx methods, also called operators, always return a new collection and never mutate the passed collection<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Ref: <a href=\"https:\/\/www.slideshare.net\/rockncoder\/a-quick-intro-to-reactivex\">https:\/\/www.slideshare.net\/rockncoder\/a-quick-intro-to-reactivex<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5f85\u6574\u7406\u90e8\u5206:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Observer pattern\u7814\u7a76\u8207\u5206\u6790<\/li><li>Iterator pattern\u7814\u7a76\u8207\u5206\u6790<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Ref:<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/ithelp.ithome.com.tw\/articles\/10237162\">https:\/\/ithelp.ithome.com.tw\/articles\/10237162<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/www.slideshare.net\/rockncoder\/a-quick-intro-to-reactivex\">https:\/\/www.slideshare.net\/rockncoder\/a-quick-intro-to-reactivex<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/github.com\/ReactiveX\/RxSwift\">https:\/\/github.com\/ReactiveX\/RxSwift<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4ee5\u4e0b\u8cc7\u6599\u53c3\u8003\u81eaRef\uff0c\u9019\u88e1\u50c5\u4f5c\u500b\u4eba\u5b78\u7fd2\u6574\u7406 Definition of ReactiveX Advantage [&hellip;]<\/p>\n","protected":false},"author":139426785,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_coblocks_attr":"","_coblocks_dimensions":"","_coblocks_responsive_height":"","_coblocks_accordion_ie_support":"","_postgate_lock_override":"inherit","_postgate_lock_mode":"inherit","_postgate_preview_mode":"inherit","_postgate_preview_limit":0,"_postgate_product_ids":[],"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_seo_schema_type":"","_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":true,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_wpcom_ai_launchpad_first_post":false,"_jetpack_hide_featured_image":false,"_jetpack_featured_image_duplicate_dismissed":0,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"{title}\n\n{excerpt}\n\n{url}","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"_wpas_customize_per_network":false,"jetpack_post_was_ever_published":false},"categories":[37324612,373736456,55138170,1],"tags":[],"post_folder":[],"class_list":["post-4431","post","type-post","status-publish","format-standard","hentry","category-ios-development","category-rxswift","category-technology-sharing","category-uncategorized"],"jetpack_publicize_connections":[],"jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/paR5vw-19t","jetpack-related-posts":[{"id":4594,"url":"https:\/\/1989lifejourney.com\/?p=4594","url_meta":{"origin":4431,"position":0},"title":"RxSwift\u6574\u7406","author":"Kevin","date":"25 2 \u6708, 2022","format":false,"excerpt":"RxSwift Definition:Use concept of Observer pattern\u2026","rel":"","context":"\u5728\u300cIOS Development\u300d\u4e2d","block_context":{"text":"IOS Development","link":"https:\/\/1989lifejourney.com\/?cat=37324612"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":5858,"url":"https:\/\/1989lifejourney.com\/?p=5858","url_meta":{"origin":4431,"position":1},"title":"IOS data binding","author":"Kevin","date":"5 9 \u6708, 2022","format":false,"excerpt":"\u4ee5\u4e0b\u8cc7\u6599\u7686\u4f86\u81ea[Ref 1]\uff0c\u9019\u88e1\u50c5\u4f5c\u500b\u4eba\u7b46\u8a18\u6574\u7406: Data Binding is simply \u2026","rel":"","context":"\u5728\u300cIOS Development\u300d\u4e2d","block_context":{"text":"IOS Development","link":"https:\/\/1989lifejourney.com\/?cat=37324612"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":6444,"url":"https:\/\/1989lifejourney.com\/?p=6444","url_meta":{"origin":4431,"position":2},"title":"RxSwift\u5b78\u7fd2\u91cd\u9ede\u6574\u7406","author":"Kevin","date":"11 1 \u6708, 2023","format":false,"excerpt":"RxSwift\u5b78\u7fd2\u91cd\u9ede: \u4e0d\u53ef\u5fd8\u8a18\u7684\u524d\u63d0: \u4e8b\u4ef6\u662f\u5728\u54ea\u4e00\u689dthread\u4e0a\u57f7\u884c(ex: main th\u2026","rel":"","context":"\u5728\u300cIOS Development\u300d\u4e2d","block_context":{"text":"IOS Development","link":"https:\/\/1989lifejourney.com\/?cat=37324612"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":3895,"url":"https:\/\/1989lifejourney.com\/?p=3895","url_meta":{"origin":4431,"position":3},"title":"Functional Reactive Programming","author":"Kevin","date":"2 12 \u6708, 2021","format":false,"excerpt":"What is functional reactive programming (FRP)?FRP \u2026","rel":"","context":"\u5728\u300cIOS Development\u300d\u4e2d","block_context":{"text":"IOS Development","link":"https:\/\/1989lifejourney.com\/?cat=37324612"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":4442,"url":"https:\/\/1989lifejourney.com\/?p=4442","url_meta":{"origin":4431,"position":4},"title":"RxSwift\u4f7f\u7528","author":"Kevin","date":"17 2 \u6708, 2022","format":false,"excerpt":"\u4ee5\u4e0b\u8cc7\u6599\u53c3\u8003\u81eaRef\uff0c\u9019\u88e1\u50c5\u4f5c\u500b\u4eba\u5b78\u7fd2\u6574\u7406 Rx\u7684\u4e00\u500b\u6838\u5fc3\u89c0\u5ff5\u5c31\u662fobservable\uff08\u6216\u53ebsequ\u2026","rel":"","context":"\u5728\u300cIOS Development\u300d\u4e2d","block_context":{"text":"IOS Development","link":"https:\/\/1989lifejourney.com\/?cat=37324612"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":4536,"url":"https:\/\/1989lifejourney.com\/?p=4536","url_meta":{"origin":4431,"position":5},"title":"Reactive programming","author":"Kevin","date":"23 2 \u6708, 2022","format":false,"excerpt":"\u5b9a\u7fa9: Reactive programming is a declarative programm\u2026","rel":"","context":"\u5728\u300cIOS Development\u300d\u4e2d","block_context":{"text":"IOS Development","link":"https:\/\/1989lifejourney.com\/?cat=37324612"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/1989lifejourney.com\/wp-content\/uploads\/2022\/02\/e688aae59c96-2022-02-23-e4b88be58d881.53.06.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/1989lifejourney.com\/wp-content\/uploads\/2022\/02\/e688aae59c96-2022-02-23-e4b88be58d881.53.06.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/1989lifejourney.com\/wp-content\/uploads\/2022\/02\/e688aae59c96-2022-02-23-e4b88be58d881.53.06.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/1989lifejourney.com\/wp-content\/uploads\/2022\/02\/e688aae59c96-2022-02-23-e4b88be58d881.53.06.png?resize=700%2C400&ssl=1 2x, https:\/\/i0.wp.com\/1989lifejourney.com\/wp-content\/uploads\/2022\/02\/e688aae59c96-2022-02-23-e4b88be58d881.53.06.png?resize=1050%2C600&ssl=1 3x, https:\/\/i0.wp.com\/1989lifejourney.com\/wp-content\/uploads\/2022\/02\/e688aae59c96-2022-02-23-e4b88be58d881.53.06.png?resize=1400%2C800&ssl=1 4x"},"classes":[]}],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/1989lifejourney.com\/index.php?rest_route=\/wp\/v2\/posts\/4431","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/1989lifejourney.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/1989lifejourney.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/1989lifejourney.com\/index.php?rest_route=\/wp\/v2\/users\/139426785"}],"replies":[{"embeddable":true,"href":"https:\/\/1989lifejourney.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4431"}],"version-history":[{"count":24,"href":"https:\/\/1989lifejourney.com\/index.php?rest_route=\/wp\/v2\/posts\/4431\/revisions"}],"predecessor-version":[{"id":5225,"href":"https:\/\/1989lifejourney.com\/index.php?rest_route=\/wp\/v2\/posts\/4431\/revisions\/5225"}],"wp:attachment":[{"href":"https:\/\/1989lifejourney.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4431"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/1989lifejourney.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4431"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/1989lifejourney.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4431"},{"taxonomy":"post_folder","embeddable":true,"href":"https:\/\/1989lifejourney.com\/index.php?rest_route=%2Fwp%2Fv2%2Fpost_folder&post=4431"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}