Integrate Combine Into an App
One of the best ways to test your Combine skills is to put them to use in an app! In this course, put the finishing touches on a Chuck Norris jokes app, using Combine to fetch jokes, translate them into another language, and even - as a bonus - learn how to use Combine with Core Data to save jokes to tell your friends later. By Josh Steele.
This is part of the Declarative Programming with Combine learning path. View path.
Who is this for?
This course is suitable for iOS developers who have some experience with Combine and SwiftUI, and want to learn about developing a complete app with those technologies.
- Reactive Programming
- Core Data
- Unit Testing
Part 1: Define a View Model
Knowing Combine’s feature set, you can identify areas of your code where using a Combine pipeline would be most beneficial, which will allow you to keep your app up to date and provide the best user experience possible. In this episode, use the requirements of the Jokes app to see where Combine will work the best.
Property Wrappers in Combine allow state to easily become published to your app’s SwiftUI views. In this episode,
you’ll learn how to use the
@Published property wrapper and the
ObservableObject protocol to get your models
ready to be used by SwiftUI.
Your app has to get data from a source in order to use it. The Publisher-Subscriber paradigm seen in Combine seems like a natural fit here, so in this episode, learn how to create custom publishers to help deliver jokes and translations to your model.
Publishers are only part of the Combine pipeline; they have to be subscribed to in order to start the data flowing. In this episode, learn how the Publishers you’ve made can be used in the ViewModel that will drive the user interface.
Once you have a model publishing values by adopting
ObservableObject, you can use the
@ObservedObject property wrapper in SwiftUI to update your views as values change.
Part 2: Integrate with Core Data & Unit Test
While SwiftUI and Swift make heavy use of Structs, Core Data still uses classes. In this episode, learn some techniques to convert between the two types so you can save your jokes into the database, and delete them when your user just doesn’t find a joke funny anymore.
The Core Data stack is the main interface with Core Data for your entire app. In this episode, you’ll initialize the Core Data stack, and inject it into the SwiftUI Environment so you can fetch values from it later.
FetchRequests allow you to get data from a Core Data database for a given entity type, sort method, and predicate. In this episode you’ll use a special property wrapper, @FetchRequest, to bind a FetchRequest with the Core Data database, allowing you to keep your UI elements up to date.
As with all other code, your Combine pipeline code can, and should be, tested. The Given-When-Then pattern is a great way to layout your test code, ensuring that your code works as intended.