{"id":2631,"date":"2018-04-20T17:28:30","date_gmt":"2018-04-20T13:28:30","guid":{"rendered":"https:\/\/blog.alwawee.ru\/?p=2631"},"modified":"2018-04-20T17:32:06","modified_gmt":"2018-04-20T13:32:06","slug":"using-raccommand-and-creating-custom-racsignals-in-objective-c","status":"publish","type":"post","link":"https:\/\/blog.alwawee.ru\/?p=2631","title":{"rendered":"Using RACCommand and creating custom RACSignals in Objective C"},"content":{"rendered":"<p>To bind some action to the button click one can use just a custom RACSignal. But then you have to add three more things in every case:<br \/>\n1. Disable button, while action is performing;<br \/>\n2. Show, that action is in progress;<br \/>\n3. Disable button, if input is incorrect and button should not be able to be tapped. <\/p>\n<p>Instead, if you use RACCommand, you get all these things in a box. <\/p>\n<p>Let&#8217;s say, we want to bind RACCommand verifyPhoneCommand to a button. We can create a command in a lazy property in a ViewModel: <\/p>\n<pre>\r\n- (RACCommand *)verifyPhoneCommand\r\n{\r\n    if (! _verifyPhoneCommand) {\r\n        _verifyPhoneCommand = [[RACCommand alloc] initWithEnabled:self.phoneValidSignal\r\n                                                      signalBlock:^RACSignal * _Nonnull(id  _Nullable input) {\r\n                                                          return [self verifyPhone];\r\n                                                      }];\r\n    }\r\n    return _verifyPhoneCommand;\r\n}\r\n<\/pre>\n<p>Here phoneValidSignal is this signal, that is formed by observing a phone variable: <\/p>\n<pre>\r\n- (RACSignal *)phoneValidSignal\r\n{\r\n    if (!_phoneValidSignal) {\r\n        _phoneValidSignal = [RACObserve(self, phone) map:^id(NSString *phone) {\r\n            return @(phone.length > 0);\r\n        }];\r\n    }\r\n    return _phoneValidSignal;\r\n}\r\n<\/pre>\n<p>We pass a RACSignal verifyPhone to the RACCommand to do a job: <\/p>\n<pre>\r\n- (RACSignal *)verifyPhone\r\n{\r\n    return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {\r\n\r\n        [[PhoneManager sharedManager]\r\n         requestVerifyPhone:self.phone\r\n         success:^{\r\n\r\n             [subscriber sendNext:@(YES)];\r\n             [subscriber sendCompleted];\r\n             \r\n         } failure:^(NSError *error) {\r\n             \r\n             [subscriber sendError:error];\r\n             \r\n         }];\r\n        \r\n        return nil;\r\n    }];\r\n}\r\n<\/pre>\n<p>This is how we bind a RACCommand to a button: <\/p>\n<pre>\r\nself.smsRequestButton.rac_command = self.viewModel.verifyPhoneCommand;\r\n<\/pre>\n<p>To track execution of a RACCommand and show an activity indicator we can use executing property: <\/p>\n<pre>\r\n[self.viewModel.verifyPhoneCommand.executing subscribeNext:^(NSNumber*  _Nullable x) {\r\n        if (x.boolValue) {\r\n            [self.spinnerView startAnimating];\r\n        }\r\n        else {\r\n            [self.spinnerView stopAnimating];\r\n        }\r\n    }];\r\n<\/pre>\n<p>If errors happen during RACCommand execution we can process those errors: <\/p>\n<pre>\r\n[self.viewModel.verifyPhoneCommand.errors subscribeNext:^(NSError * _Nullable error) {\r\n        [ErrorUtil showError:error from:self onRepeat:^{\r\n            \r\n        }];\r\n    }];\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>To bind some action to the button click one can use just a custom RACSignal. But then you have to add three more things in every case: 1. Disable button, while action is performing; 2. Show, that action is in progress; 3. Disable button, if input is incorrect and button should not be able to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[206,205,194],"class_list":["post-2631","post","type-post","status-publish","format-standard","hentry","category-ios","tag-objective-c","tag-raccommand","tag-reactivecocoa"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.6 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Using RACCommand and creating custom RACSignals in Objective C - Denis Kutlubaev<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/blog.alwawee.ru\/?p=2631\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Using RACCommand and creating custom RACSignals in Objective C - Denis Kutlubaev\" \/>\n<meta property=\"og:description\" content=\"To bind some action to the button click one can use just a custom RACSignal. But then you have to add three more things in every case: 1. Disable button, while action is performing; 2. Show, that action is in progress; 3. Disable button, if input is incorrect and button should not be able to [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/blog.alwawee.ru\/?p=2631\" \/>\n<meta property=\"og:site_name\" content=\"Denis Kutlubaev\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/alwaweemobile\" \/>\n<meta property=\"article:published_time\" content=\"2018-04-20T13:28:30+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2018-04-20T13:32:06+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/blog.alwawee.com\/?attachment_id=1982\" \/>\n<meta name=\"author\" content=\"Denis Kutlubaev\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Denis Kutlubaev\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=2631#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=2631\"},\"author\":{\"name\":\"Denis Kutlubaev\",\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/#\\\/schema\\\/person\\\/58f1f1f57155b48afab83ba53b3e26a7\"},\"headline\":\"Using RACCommand and creating custom RACSignals in Objective C\",\"datePublished\":\"2018-04-20T13:28:30+00:00\",\"dateModified\":\"2018-04-20T13:32:06+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=2631\"},\"wordCount\":162,\"commentCount\":2,\"keywords\":[\"Objective C\",\"RACCommand\",\"ReactiveCocoa\"],\"articleSection\":[\"iOS\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/blog.alwawee.ru\\\/?p=2631#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=2631\",\"url\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=2631\",\"name\":\"Using RACCommand and creating custom RACSignals in Objective C - Denis Kutlubaev\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/#website\"},\"datePublished\":\"2018-04-20T13:28:30+00:00\",\"dateModified\":\"2018-04-20T13:32:06+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/#\\\/schema\\\/person\\\/58f1f1f57155b48afab83ba53b3e26a7\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=2631#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/blog.alwawee.ru\\\/?p=2631\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=2631#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/blog.alwawee.ru\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Using RACCommand and creating custom RACSignals in Objective C\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/#website\",\"url\":\"https:\\\/\\\/blog.alwawee.ru\\\/\",\"name\":\"Denis Kutlubaev\",\"description\":\"iOS Development\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/blog.alwawee.ru\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/#\\\/schema\\\/person\\\/58f1f1f57155b48afab83ba53b3e26a7\",\"name\":\"Denis Kutlubaev\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/346de75d27c818be0aa7fddeada2e50e30cc15b4da6b3b8110c5d766cd2afa34?s=96&d=mm&r=pg\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/346de75d27c818be0aa7fddeada2e50e30cc15b4da6b3b8110c5d766cd2afa34?s=96&d=mm&r=pg\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/346de75d27c818be0aa7fddeada2e50e30cc15b4da6b3b8110c5d766cd2afa34?s=96&d=mm&r=pg\",\"caption\":\"Denis Kutlubaev\"},\"description\":\"iOS Developer, creator of Tornado Browser and many other popular apps\",\"sameAs\":[\"http:\\\/\\\/dennis.alwawee.com\"],\"url\":\"https:\\\/\\\/blog.alwawee.ru\\\/?author=1\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Using RACCommand and creating custom RACSignals in Objective C - Denis Kutlubaev","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/blog.alwawee.ru\/?p=2631","og_locale":"en_US","og_type":"article","og_title":"Using RACCommand and creating custom RACSignals in Objective C - Denis Kutlubaev","og_description":"To bind some action to the button click one can use just a custom RACSignal. But then you have to add three more things in every case: 1. Disable button, while action is performing; 2. Show, that action is in progress; 3. Disable button, if input is incorrect and button should not be able to [&hellip;]","og_url":"https:\/\/blog.alwawee.ru\/?p=2631","og_site_name":"Denis Kutlubaev","article_publisher":"https:\/\/www.facebook.com\/alwaweemobile","article_published_time":"2018-04-20T13:28:30+00:00","article_modified_time":"2018-04-20T13:32:06+00:00","og_image":[{"url":"http:\/\/blog.alwawee.com\/?attachment_id=1982","type":"","width":"","height":""}],"author":"Denis Kutlubaev","twitter_misc":{"Written by":"Denis Kutlubaev","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/blog.alwawee.ru\/?p=2631#article","isPartOf":{"@id":"https:\/\/blog.alwawee.ru\/?p=2631"},"author":{"name":"Denis Kutlubaev","@id":"https:\/\/blog.alwawee.ru\/#\/schema\/person\/58f1f1f57155b48afab83ba53b3e26a7"},"headline":"Using RACCommand and creating custom RACSignals in Objective C","datePublished":"2018-04-20T13:28:30+00:00","dateModified":"2018-04-20T13:32:06+00:00","mainEntityOfPage":{"@id":"https:\/\/blog.alwawee.ru\/?p=2631"},"wordCount":162,"commentCount":2,"keywords":["Objective C","RACCommand","ReactiveCocoa"],"articleSection":["iOS"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/blog.alwawee.ru\/?p=2631#respond"]}]},{"@type":"WebPage","@id":"https:\/\/blog.alwawee.ru\/?p=2631","url":"https:\/\/blog.alwawee.ru\/?p=2631","name":"Using RACCommand and creating custom RACSignals in Objective C - Denis Kutlubaev","isPartOf":{"@id":"https:\/\/blog.alwawee.ru\/#website"},"datePublished":"2018-04-20T13:28:30+00:00","dateModified":"2018-04-20T13:32:06+00:00","author":{"@id":"https:\/\/blog.alwawee.ru\/#\/schema\/person\/58f1f1f57155b48afab83ba53b3e26a7"},"breadcrumb":{"@id":"https:\/\/blog.alwawee.ru\/?p=2631#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/blog.alwawee.ru\/?p=2631"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/blog.alwawee.ru\/?p=2631#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/blog.alwawee.ru\/"},{"@type":"ListItem","position":2,"name":"Using RACCommand and creating custom RACSignals in Objective C"}]},{"@type":"WebSite","@id":"https:\/\/blog.alwawee.ru\/#website","url":"https:\/\/blog.alwawee.ru\/","name":"Denis Kutlubaev","description":"iOS Development","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/blog.alwawee.ru\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/blog.alwawee.ru\/#\/schema\/person\/58f1f1f57155b48afab83ba53b3e26a7","name":"Denis Kutlubaev","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/346de75d27c818be0aa7fddeada2e50e30cc15b4da6b3b8110c5d766cd2afa34?s=96&d=mm&r=pg","url":"https:\/\/secure.gravatar.com\/avatar\/346de75d27c818be0aa7fddeada2e50e30cc15b4da6b3b8110c5d766cd2afa34?s=96&d=mm&r=pg","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/346de75d27c818be0aa7fddeada2e50e30cc15b4da6b3b8110c5d766cd2afa34?s=96&d=mm&r=pg","caption":"Denis Kutlubaev"},"description":"iOS Developer, creator of Tornado Browser and many other popular apps","sameAs":["http:\/\/dennis.alwawee.com"],"url":"https:\/\/blog.alwawee.ru\/?author=1"}]}},"_links":{"self":[{"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=\/wp\/v2\/posts\/2631","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2631"}],"version-history":[{"count":3,"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=\/wp\/v2\/posts\/2631\/revisions"}],"predecessor-version":[{"id":2634,"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=\/wp\/v2\/posts\/2631\/revisions\/2634"}],"wp:attachment":[{"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2631"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2631"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2631"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}