From 95ba6123346c75af3a64aab7dd32e084b2e8209d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Garci=CC=81a?= Date: Sun, 21 Jun 2015 10:57:41 +0200 Subject: [PATCH] retry an take added --- .../Contents.swift | 45 +++++++++++++++++++ .../Contents.swift | 17 ++++++- .../Contents.swift | 2 +- .../Contents.swift | 25 +---------- .../Preprocessor.xcodeproj/project.pbxproj | 2 + RxCocoa/RxCocoa.xcodeproj/project.pbxproj | 1 + RxTests/RxTests.xcodeproj/project.pbxproj | 1 + 7 files changed, 68 insertions(+), 25 deletions(-) diff --git a/Playgrounds/ObservablesOperators/Observables+Error Handling.playground/Contents.swift b/Playgrounds/ObservablesOperators/Observables+Error Handling.playground/Contents.swift index 77e2fbf7..a5d2d3ce 100644 --- a/Playgrounds/ObservablesOperators/Observables+Error Handling.playground/Contents.swift +++ b/Playgrounds/ObservablesOperators/Observables+Error Handling.playground/Contents.swift @@ -51,3 +51,48 @@ example("catch") { } + + +/*: +## `retry` + +If a source Observable emits an error, resubscribe to it in the hopes that it will complete without error +[More info in reactive.io website]( http://reactivex.io/documentation/operators/retry.html ) +*/ + +example("retry") { + + var count = 1 // bad practice, only for example purposes + let observable: Observable = create { observer in + let error = NSError(domain: "Test", code: 0, userInfo: nil) + sendNext(observer, 0) + sendNext(observer, 1) + sendNext(observer, 2) + if count < 2 { + sendError(observer, error) + count++ + } + sendNext(observer, 3) + sendNext(observer, 4) + sendNext(observer, 5) + sendCompleted(observer) + + return AnonymousDisposable {} + } + + + observable + >- retry + >- subscribe { event in + switch event { + case .Next(let box): + println("\(box.value)") + case .Completed: + println("completed") + case .Error(let error): + println("\(error)") + } + } + + +} diff --git a/Playgrounds/ObservablesOperators/Observables+Filtering.playground/Contents.swift b/Playgrounds/ObservablesOperators/Observables+Filtering.playground/Contents.swift index 547394ea..813b79d6 100644 --- a/Playgrounds/ObservablesOperators/Observables+Filtering.playground/Contents.swift +++ b/Playgrounds/ObservablesOperators/Observables+Filtering.playground/Contents.swift @@ -37,4 +37,19 @@ example("distinctUntilChanged") { >- subscribeNext { value in println("\(value)") } -} \ No newline at end of file +} + +/*: +### `take` +Emit only the first n items emitted by an Observable +[More info in reactive.io website]( http://reactivex.io/documentation/operators/take.html ) +*/ + +example("take") { + let distinctUntilChangedSubscriber = returnElements(1, 2, 3, 4, 5, 6) + >- take(3) + >- subscribeNext { value in + println("\(value)") + } +} + diff --git a/Playgrounds/ObservablesOperators/Observables+Transforming.playground/Contents.swift b/Playgrounds/ObservablesOperators/Observables+Transforming.playground/Contents.swift index 3209aa25..5bff78ed 100644 --- a/Playgrounds/ObservablesOperators/Observables+Transforming.playground/Contents.swift +++ b/Playgrounds/ObservablesOperators/Observables+Transforming.playground/Contents.swift @@ -99,4 +99,4 @@ example("scan") { >- subscribeNext { println($0) } -} \ No newline at end of file +} diff --git a/Playgrounds/ObservablesOperators/Observables+Utility.playground/Contents.swift b/Playgrounds/ObservablesOperators/Observables+Utility.playground/Contents.swift index a91ea8e2..45745bfc 100644 --- a/Playgrounds/ObservablesOperators/Observables+Utility.playground/Contents.swift +++ b/Playgrounds/ObservablesOperators/Observables+Utility.playground/Contents.swift @@ -85,7 +85,7 @@ example("subscribeError") { ### `do` Returns the same source Observable but the given closure responsible for the actions to perform when the even is produced. The gived closure obtain the event produced by the source observable -[More info in reactive.io website](http://reactivex.io/documentation/operators/do.html) +[More info in reactive.io website]( http://reactivex.io/documentation/operators/do.html ) */ example("do") { @@ -108,7 +108,7 @@ example("do") { /*: ### `doOnNext` It is a variant of the `do` operator. Returns the same source Observable but the given closure responsible for the actions to perform when the Next even is produced. The gived closure obtain the value of the Next event produced by the source observable. -[More info in reactive.io website](http://reactivex.io/documentation/operators/do.html) +[More info in reactive.io website]( http://reactivex.io/documentation/operators/do.html ) */ example("doOnNext") { @@ -127,24 +127,3 @@ example("doOnNext") { sendNext(intOb1, 1) } - - -/*: -### `observeSingleOn` -Specify the Scheduler on which an observer will observe this Observable -[More info in reactive.io website](http://reactivex.io/documentation/operators/observeon.html) -*/ - -//TODO: Do not work in playgrounds -example("observeSingleOn") { - let intOb1 = Subject() - - intOb1 - >- observeSingleOn(MainScheduler.sharedInstance) - >- subscribeNext { int in - println(int) - } - - sendNext(intOb1, 1) -} - diff --git a/Preprocessor/Preprocessor.xcodeproj/project.pbxproj b/Preprocessor/Preprocessor.xcodeproj/project.pbxproj index 55fd1f4c..03b95c3e 100644 --- a/Preprocessor/Preprocessor.xcodeproj/project.pbxproj +++ b/Preprocessor/Preprocessor.xcodeproj/project.pbxproj @@ -88,6 +88,7 @@ C811086F1AF5114D001C13E4 /* Project object */ = { isa = PBXProject; attributes = { + LastSwiftUpdateCheck = 0700; LastUpgradeCheck = 0630; ORGANIZATIONNAME = "Krunoslav Zaher"; TargetAttributes = { @@ -236,6 +237,7 @@ C81108801AF5114D001C13E4 /* Release */, ); defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; }; /* End XCConfigurationList section */ }; diff --git a/RxCocoa/RxCocoa.xcodeproj/project.pbxproj b/RxCocoa/RxCocoa.xcodeproj/project.pbxproj index 39000a82..c6dd9843 100644 --- a/RxCocoa/RxCocoa.xcodeproj/project.pbxproj +++ b/RxCocoa/RxCocoa.xcodeproj/project.pbxproj @@ -253,6 +253,7 @@ C81553D51A98AB4A00C63152 /* Project object */ = { isa = PBXProject; attributes = { + LastSwiftUpdateCheck = 0700; LastUpgradeCheck = 0610; ORGANIZATIONNAME = "Krunoslav Zaher"; TargetAttributes = { diff --git a/RxTests/RxTests.xcodeproj/project.pbxproj b/RxTests/RxTests.xcodeproj/project.pbxproj index bb2b5317..7b6ed762 100644 --- a/RxTests/RxTests.xcodeproj/project.pbxproj +++ b/RxTests/RxTests.xcodeproj/project.pbxproj @@ -323,6 +323,7 @@ C81108161AF50DDA001C13E4 /* Project object */ = { isa = PBXProject; attributes = { + LastSwiftUpdateCheck = 0700; LastUpgradeCheck = 0630; TargetAttributes = { C811081F1AF50E11001C13E4 = {