Today: Started on Chapter 6 of Practical Combine.
Thoughts: n/a
Key concepts:
-
Combine has a
share
operator that shares the output values from the upstream Publisher with multiple subscribers. -
The resulting Publisher is a class instance (reference semantics)
-
This is handy for example where you want to make a single network call that multiple subscribers can subscribe to but without making a new network call for each subscriber.
-
Combine has a
decode
operator that can make short work of decoding JSON.// Some publisher that emits Data values .decode(type: T.self, decoder: JSONDecoder()) // where T conforms to Decodable and // decoder conforms to TopLevelDecoder
Links:
Today: Finished Chapter 6 of Practical Combine. Tested the replacement Grow HAT Mini from Pimoroni.
Thoughts: Pimoroni has excellent customer service.
Key concepts:
- Apple introduced a setting in iOS 13+ called Low Data Mode that the user uses to indicate that apps should opt-in to use less network data.
- To opt-in, you set the property
allowsConstrainedNetworkAccess = false
on theURLSessionConfiguration
or on an individualURLRequest
object. - Requests made while Low Data Mode is enabled while raise an URLError with the networkUnavailableReason set to .constrained.
- Apple also introduced
allowsExpensiveNetworkAccess
(on URLSessionConfiguration and URLRequest) that will raise an URLError when the user is on an expensive network. Wonder how they determine this.
Links:
Today: Started on Chapter 7 of Practical Combine.
Thoughts: I like that Combine comes with Futures & Promises and I no longer have to use other frameworks for this.
Key concepts:
-
You can create custom Publishers in the form of Subject objects like CurrentValueSubject and PassthroughSubject.
-
Combine can also do
Future
s andPromise
s. -
A Future is a Publisher that will emit a single value and complete immediately.
-
It can never emit more than one value.
-
Recap: Swift introduced a Result generic type.
@frozen enum Result<Success, Failure> where Failure : Error
-
Example creation of a Future
func createFuture() -> Future<Int, Never> { return Future { promise in promise(.success(42)) } } Output: Int Failure: Never Future's init takes a closure that is passed a Promise Future has: typealias Promise = (Result<Output, Failure>) -> Void
-
Thus a Future takes a closure param at init, which in turn will be passed a Promise closure, which will be called in the future with a Result. Where the Result's Success type must match the Future's Output type and the Result's Failure must match the Future's Failure type. <-- Did you get all that? It is a lot to unpack.
-
Recap: Publishers will not do any work unless there are Subscribers.
-
Futures on the other hand will execute the work as soon as they are created.
-
However they will emit the same result to every subscriber being added. Thus do the work once and report many.
-
You can wrap a Future in a Deferred publisher and thus the Future will behave like a "normal" publisher. I.e. Only do work when there are publishers and also do the work for each subscriber.
Links:
Today: Finished Chapter 7 & 8 of Practical Combine
Thoughts: Paragraph or two
Key concepts:
- Practical Combine Ch7 has a nice example of asking for permission to use notifications as well as an example of wrapping Core Data fetches in a Future.
- Combine has a
receive(on:)
operator that you can use to specify on which Scheduler to receive the values on. - The
Scheduler
protocol is conformed byDispatchQueue
,OperationQueue
andRunLoop
. - There is also
ImmediateScheduler
that will perform any work immediately and can't be delayed. - Combine expects schedulers to operate on a serial queue.
- The default scheduler will emit values on the thread they were generated on.
- Combine also has a
subscribe(on:)
operator that you use to specify on which Scheduler subscribers are registered with the publisher.
Links:
Today: Finished Chapter 9 of Practical Combine
Thoughts: I am strugling tonight to download information into my brain. TGIF.
Key concepts:
-
It is recommended that you don't write your own custom Publishers and Subscribers unless you really need to.
-
In Combine the subscribers are in charge of how many values they want to receive.
-
A Subscriber is handed a
Subscription
object via thereceive(subscription:)
method. -
Backpressure is managed via this subscription object.
func receive(subscription: Subscription) { subscription.request(.max(1)) ...
Links:
n/a
Today: Going to try kratky method using "upcycled" juice / soya cartons. Project for the kids. Continued building my mobile bench.
Thoughts: n/a
Key concepts:
- Don't put your seedling propagators in direct sun.
- Managed to brief life back in the mustard seedlings by giving them water and moving them out of direct sun light.
- Always support your work and offcut pieces when cutting with any power tool.
- For future refence: Given the kerf of my Erbauer track saw. When cutting on the waste side, use the T-ruler and add 2mm and 3 holes down ( + 0.75mm)
Links:
Today: Finished Chapter 10 of Practical Combine. Continued with the mobile bench build. Managed to get the full cut list finished.
Thoughts: It has been a good weekend with good weather and I managed to work on a couple of projects.
Week 6 complete
Key concepts:
- Combine has a
print
operator to help debug what a publisher is doing. - Timelane is free open source tool for debugging combine.
Links: