Skip to content

Commit 6642035

Browse files
committed
Use ImmediateOnMainExecutionContext as default context for operations
1 parent e11bdc6 commit 6642035

File tree

6 files changed

+19
-15
lines changed

6 files changed

+19
-15
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ Operation can send any number of `.Next` events followed by one terminating even
172172
Creating the operation doesn't do any work by itself. To start producing results, operation has to be started. Operation will be automatically started when you register an observer to it.
173173

174174
```swift
175-
fetchImage(url: ...).observe(on: Queue.main.contex) { event in
175+
fetchImage(url: ...).observe { event in
176176
switch event {
177177
case .Next(let image):
178178
print("Operation produced an image \(image).")
@@ -184,7 +184,7 @@ fetchImage(url: ...).observe(on: Queue.main.contex) { event in
184184
}
185185
```
186186

187-
> When you register the observer to the operation you must explicitly provide execution context onto which events will be dispatched. This differs from observing observables where the main queue context is set as the default one because it encourages us to think about threads (queues) and forces us to explicitly define where do we want to perform the side effects.
187+
> Observers registered with `observe` method will be by default invoked on the main thread (queue). You can change default behaviour by passing another [execution context](#threading) to the `observe` method.
188188
189189
The observer you register with the operation is actually the one that will be passed to the closure that was provided in operation constructor (the one that does the actual work) - just wrapped into a struct that simplifies sending result. You see how the operation is just a light wrapper around a closure, but that abstraction enables powerful paradigm.
190190

@@ -193,7 +193,7 @@ The observer you register with the operation is actually the one that will be pa
193193
When you're interested in just results of the operation and you don't care when it completes or if it fails, you can use `*Next` family of methods. To observe results of the operation, you would use `observeNext`.
194194

195195
```swift
196-
fetchImage(url: ...).observeNext(on: Queue.main.contex) { image in
196+
fetchImage(url: ...).observeNext { image in
197197
imageView.image = image
198198
}
199199
```
@@ -212,7 +212,7 @@ fetchImage(url: ...).bindNextTo(imageView)
212212
Whenever the observer is registered, the operation starts all over again. To share results of a single operation run, use `shareNext` method.
213213

214214
```swift
215-
let image = fetchImage(url: ...).shareNext(on: Queue.main.context)
215+
let image = fetchImage(url: ...).shareNext
216216
image.bindTo(imageView1)
217217
image.bindTo(imageView2)
218218
```
@@ -250,7 +250,7 @@ authenticate(username: ..., password: ...)
250250
.flatMap(.Latest) { token in
251251
return fetchCurrentUser(token)
252252
}
253-
.observeNext(on: Queue.main.context) { user in
253+
.observeNext { user in
254254
print("Authenticated as \(user.fullname).")
255255
}
256256
```

ReactiveKit.podspec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
Pod::Spec.new do |s|
22
s.name = "ReactiveKit"
3-
s.version = "1.0.2"
3+
s.version = "1.0.3"
44
s.summary = "A Swift Reactive Programming Framework"
55
s.description = "ReactiveKit is a collection of Swift frameworks for reactive and functional reactive programming."
66
s.homepage = "https://github.com/ReactiveKit/ReactiveKit"
77
s.license = 'MIT'
88
s.author = { "Srdan Rasic" => "[email protected]" }
9-
s.source = { :git => "https://github.com/ReactiveKit/ReactiveKit.git", :tag => "v1.0.2" }
9+
s.source = { :git => "https://github.com/ReactiveKit/ReactiveKit.git", :tag => "v1.0.3" }
1010

1111
s.ios.deployment_target = '8.0'
1212
s.osx.deployment_target = '10.9'

ReactiveKit.xcodeproj/project.pbxproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@
196196
ECBCCE061BEB6BBE00723476 /* StreamType.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StreamType.swift; sourceTree = "<group>"; };
197197
ECBCCE2A1BEB6BE100723476 /* NoError.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = NoError.swift; path = ../Operation/NoError.swift; sourceTree = "<group>"; };
198198
ECBCCE2B1BEB6BE100723476 /* Stream+Operation.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Stream+Operation.swift"; sourceTree = "<group>"; };
199-
ECBCCE2C1BEB6BE100723476 /* Operation.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Operation.swift; sourceTree = "<group>"; };
199+
ECBCCE2C1BEB6BE100723476 /* Operation.swift */ = {isa = PBXFileReference; fileEncoding = 4; indentWidth = 2; lastKnownFileType = sourcecode.swift; path = Operation.swift; sourceTree = "<group>"; tabWidth = 2; };
200200
ECBCCE2D1BEB6BE100723476 /* OperationEvent.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OperationEvent.swift; sourceTree = "<group>"; };
201201
ECBCCE2E1BEB6BE100723476 /* OperationSink.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OperationSink.swift; sourceTree = "<group>"; };
202202
/* End PBXFileReference section */

ReactiveKit/Info.plist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<key>CFBundlePackageType</key>
1616
<string>FMWK</string>
1717
<key>CFBundleShortVersionString</key>
18-
<string>1.0.2</string>
18+
<string>1.0.3</string>
1919
<key>CFBundleSignature</key>
2020
<string>????</string>
2121
<key>CFBundleVersion</key>

ReactiveKit/Operation/Operation.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,11 @@ public struct Operation<Value, Error: ErrorType>: OperationType {
4646
})
4747
}
4848
}
49-
49+
50+
public func observe(observer: OperationEvent<Value, Error> -> ()) -> DisposableType {
51+
return observe(on: ImmediateOnMainExecutionContext, observer: observer)
52+
}
53+
5054
public func observe(on context: ExecutionContext, observer: OperationEvent<Value, Error> -> ()) -> DisposableType {
5155
return stream.observe(on: context, observer: observer)
5256
}
@@ -102,7 +106,7 @@ public extension OperationType {
102106
}
103107
}
104108

105-
public func observeNext(on context: ExecutionContext, observer: Value -> ()) -> DisposableType {
109+
public func observeNext(on context: ExecutionContext = ImmediateOnMainExecutionContext, observer: Value -> ()) -> DisposableType {
106110
return self.observe(on: context) { event in
107111
switch event {
108112
case .Next(let event):
@@ -112,7 +116,7 @@ public extension OperationType {
112116
}
113117
}
114118

115-
public func observeError(on context: ExecutionContext, observer: Error -> ()) -> DisposableType {
119+
public func observeError(on context: ExecutionContext = ImmediateOnMainExecutionContext, observer: Error -> ()) -> DisposableType {
116120
return self.observe(on: context) { event in
117121
switch event {
118122
case .Failure(let error):
@@ -123,7 +127,7 @@ public extension OperationType {
123127
}
124128

125129
@warn_unused_result
126-
public func shareNext(limit: Int = Int.max, context: ExecutionContext = Queue.main.context) -> ActiveStream<Value> {
130+
public func shareNext(limit: Int = Int.max, context: ExecutionContext = ImmediateOnMainExecutionContext) -> ActiveStream<Value> {
127131
return create(limit) { observer in
128132
return self.observeNext(on: context, observer: observer)
129133
}

ReactiveKit/Streams/ActiveStream.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@ public class ActiveStream<Event>: ActiveStreamType {
6666
}
6767

6868
public func observe(observer: Observer) -> DisposableType {
69-
return observe(on: ImmediateExecutionContext, observer: observer)
69+
return observe(on: ImmediateOnMainExecutionContext, observer: observer)
7070
}
7171

72-
public func observe(on context: ExecutionContext, observer: Observer) -> DisposableType {
72+
public func observe(on context: ExecutionContext = ImmediateOnMainExecutionContext, observer: Observer) -> DisposableType {
7373
selfReference?.retain()
7474

7575
let observer = { e in context { observer(e) } }

0 commit comments

Comments
 (0)