Refining the App
There are just a couple of refinements that are missing. You're continuously adding subscribers with .store(in: &subscriptions)
without ever removing them. You'll fix that next.
Add the following line at the top of resetImages()
:
subscriptions = []
Here, you assign an empty array that will remove all the references to the unused subscriptions.
Next, add the following line at the top of stopGame()
:
subscriptions.forEach { $0.cancel() }
Here, you iterate over all subscriptions
and cancel them.
Time to build and run one last time!
I want to Combine All The Things Now!
Using Combine may seem like a great choice. It's hot, new, and first party, so why not use it now? Here are some things to think about before you go all-in:
Older iOS Versions
First of all, you need to think about your users. If you want to continue to support iOS 12, you can't use Combine. (Combine requires iOS 13 or above.)
Your Team
Reactive programming is quite a change of mindset, and there is going to be a learning curve while your team gets up to speed. Is everyone on your team as keen as you to change the way things are done?
Other SDKs
Think about the technologies your app already uses before adopting Combine. If you have other callback-based SDKs, like Core Bluetooth, you'll have to build wrappers to use them with Combine.
Gradual Integration
You can mitigate many of these concerns if you start using Combine gradually. Start from the network calls and then move to the other parts of the app. Also, consider using Combine where you currently have closures.
Where to Go From Here?
You can download the completed version of the project using the Download Materials button at the top or bottom of this tutorial.
In this tutorial, you've learned the basics behind Combine's Publisher
and Subscriber
. You also learned about using operators and timers. Congratulations, you're off to a good start with this technology!
To learn even more about using Combine, check out our book Combine: Asynchronous Programming with Swift!
If you have any questions or comments on this tutorial, please join the forum discussion below!