实例介绍
Practical Combine: An introduction to Combine with real examples
【实例截图】
【核心代码】
Learn Combine from the ground up with a solid theoretical foundation and real-world examples of where and how Combine can help you move from writing imperative code to writing reactive code that is flexible, clean and modern.
The book has a total of 13 chapters and comes with a Playground and sample projects that show the code from the chapters in a way that allows you to experiment with the code directly, and expand your knowledge about Combine even quicker.
Chapter overview
Chapter 1 - Introducing Functional Reactive Programming
Learn the basic building blocks of Combine and Functional Reactive Programming (FRP). I explain what FRP is, and how it can help you write better and cleaner code. You'll immediately see little bits and pieces of Combine code to help you see how FRP enhances code readability in certain cases. You'll also learn about some of the very superficial differences between RxSwift and Combine. This will help you understand where Combine fits in the FRP ecosystem, and it will help you choose between the two frameworks if your app is for iOS 13 and above.
Chapter 2 - Exploring publishers and subscribers
Publishers are the bread and butter of the Combine framework. Without publishers, we'd have nothing to subscribe to. In this chapter you will learn how publishers work, how you can subscribe to the values they publish, and more importantly, you'll learn what all of this means. After going through a basic example, you will learn about the lifecycle of a subscription stream, and how subscribing to a publisher works exactly.
Chapter 3 - Transforming publishers
In most cases, the values that are published by a publisher need to be modified before they can be used by a subscriber. In this chapter, I demonstrate the usage of several of Combine's built-in operators like map, flatMap, and compactMap. We'll also cover some more advanced operators like replaceNil and setFailureType. You will also learn about operators that might throw an error to end the publisher stream, like tryMap for instance. We'll wrap up the chapter by defining a custom operator on Publisher that bundles a couple of other operators in a convenient wrapper.
Chapter 4 - Updating the User Interface
In this fourth chapter, we're finally going to get truly practical. You will learn how you can use Combine to update the value of a label with a new subscriber called assign(to:on:). You will also learn how Combine can be used in an MVVM-like architecture, and how it can be used to drive a collection view's diffable data source without implementing any specific architecture. On top of updating a collection view data source, I also demonstrate how you can use Combine to download images that need to be displayed in a collection view cell. And to top it all of, you'll learn how to build a Combine-driven theme manager that can be used to allow a user to switch your app between dark- and light mode separate from the system setting.
Chapter 5 - Using Combine to respond to user input
In this chapter, you'll learn how to implement several simple bindings between UI components and a model. Once you understand how this works in UIKit, I'll demonstrate how the same principle is implemented in SwiftUI because a lot of this functionality comes for free in SwiftUI. You'll also learn how you can restrict the processing of user input through a feature called debouncing. This is especially useful if you're building a feature where user input results in expensive processing like networking for example. You will also learn about the different mechanisms that Combine offers to merge and combine the output from multiple publishers into a single publisher. By the end of this chapter, you should have a very good understanding of how publishers in Combine can be updated, and how they can be used to integrate a model and user interface element with Combine.
Chapter 6 - Using Combine for networking
Throughout the preceding chapters, I often use networking as an example of a Combine publisher. The usage of networking examples so far has been very basic though. In this chapter, you will gain a more advanced understanding of networking in Combine, and how Combine can help you support features like low data mode on iOS, or how you can implement graceful error handling. In addition to networking, you will learn about some interesting new operators like tryCatch and switchToLatest. You'll implement a pretty complicated token refresh flow with these new operators, and a couple of operators you're familiar with at this point. This chapter doesn't stop there. I will also show you how you can orchestrate several network calls at once, and merge their outputs into a single publisher. By the end of this chapter, you will have all the knowledge needed to implement a robust networking layer with Combine.
Chapter 7 - Wrapping existing asynchronous processes with Futures in Combine
Not all of your asynchronous code is easy to integrate with Combine. In this chapter, you will learn about a very special publisher called a Future. This publisher doesn't quite play by the same inferred rules as other publishers, but it's a very convenient publisher that allows us to kick off asynchronous work, and broadcast the result of this work to its subscribers. You will learn how to implement a simple Future based interface on top of UNUserNotificationCenter, and I will show you how to wrap your own asynchronous code in a Future.
Chapter 8 - Understanding Combine's Schedulers
In Combine, a lot of threading and dispatch queue related logic is hidden from you, and that's extremely convenient. However, there are times where you might need to do some manual work to make sure a certain publisher sends its events on the main queue, or you might want to make sure that it doesn't run code on the main queue. Combine abstracts dispatch queues behind schedulers. Learn how schedulers work in this chapter, and find out how they impact the code you write in your apps.
Chapter 9 - Building custom publishers and subscribers
This chapter comes with a huge warning. Nothing in this chapter should be used in production. However, we will conceptually pull apart what happens when you subscribe to a publisher using sink by reimplementing the functionality. Apple does not recommend that users of Combine do this, and it's for good reason. The fine details of how subscriptions work are hidden from us as users of the framework. Regardless, I personally like to re-implement functionality in an attempt to better understand how it might work which can lead to interesting insights.
Chapter 10 - Debugging your Combine code
Debugging is hard, especially when you're dealing with asynchronous code. In this chapter, you will learn more about Combine's `print` operator and you will learn about a community tool called Timelane that can be used to visualize the lifecycle and emitted values of your Combine subscriptions.
Chapter 11 - Testing code that uses Combine
In this chapter, you will learn how you can use XCTest to write unit tests for your Combine code. You will learn things that you should, and shouldn't do when testing Combine code. I will also show you how you can optimize your code for testability and I will show you two convenient helpers that can help you improve and clean up your test suite.
Chapter 12 - Driving publishers and flows with subjects
Learn how you can build really powerful and complex features using Combine's PassthroughSubject, CurrentValueSubject, and some clever operators. You will learn how to deal with a paginated API in Combine, how you can recursively load data and I will show you an alternative approach to retrying network calls after a token refresh.
Chapter 13 - Where to go from here
While this book should prepare you for a lot of Combine use-cases, there's also a lot that I don't cover. In this chapter, I aim to give you some insights into the features I didn't cover, and resources you might want to pick up to further deepen your Combine knowledge and expertise.
Practical Combine An introduction to Combine with real examples.pdf
相关软件
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论