Recent articles
Jump to a random postUsing custom publishers to drive SwiftUI views
Published on: June 23, 2020In SwiftUI, views can be driven by an @Published property that's part of an ObservableObject. If you've used SwiftUI and @Published before, following code should look somewhat familiar to you: class DataSource: ObservableObject { @Published var names = [String]() } struct NamesList: View { @ObservedObject var dataSource: DataSource var body: some View { List(dataSource.names, id: \.self) { name in Text(name) } } } Whenever the DataSource object's names array changes, NamesList will be automatically redrawn. That's great. Now imagine that our list of names is retrieved through the network somehow and we want to load the list of names in...
Read more...Ignore first number of elements from a publisher in Combine
Published on: June 19, 2020If you have a Combine publisher and you want to ignore the first n elements that are published by that publisher, you can use the dropFirst(_:) operator. This operator will swallow any values emitted until the threshold you specify is reached. For example, dropFirst(1) will ignore the first emitted value from a publisher: [1, 2, 3].publisher .dropFirst(1) .sink(receiveValue: { value in print(value) // 2 and 3 are printed }) For more information about dropFirst and several variations of drop like drop(while:) and drop(untilOutputFrom:) you can refer to Apple's documentation.
Read more...Recursively execute a paginated network call with Combine
Published on: June 15, 2020Last week, my attention was caught by a question that Dennis Parussini asked on Twitter. Dennis wanted to recursively make calls to a paginated API to load all pages of data before rendering UI. Since I love Combine and interesting problems I immediately started thinking about ways to achieve this using a nice, clean API. And then I realized that this is a non-trivial task that was worth exploring. In this week's post, I would like to share my thought process and solution with you, hoping you'll learn something new about Combine in the process. Understanding the problem and setting...
Read more...What’s the difference between Float and Double in Swift
Published on: June 10, 2020A Double and Float are both used to represent decimal numbers, but they do so in slightly different ways. If you initialize a decimal number in Swift using as shown below, the Swift compiler will assume that you meant to create a Double: let val = 3.123 // val is inferred to be Double The reason for this is that Double is the more precise type when comparing it to Float. A Float holds a total of 8 positions, or 32 bits. Since Double is more precise, it can hold more positions. It uses 64 bits to do this. In...
Read more...Swift Property Wrappers Explained
Published on: June 8, 2020Property wrappers are a feature that was introduced in Swift 5.1 and they play a huge role in SwiftUI and Combine which are two frameworks that shipped alongside Swift 5.1 in iOS 13. The community was quick to create some useful examples that were embraced by folks relatively quickly. As a user of property wrappers, you don't need to be concerned about what they are exactly, or how they work. All that you need to know is how you can use them. However, if you're curious how property wrappers work on the inside, this is just the post for you....
Read more...Five tips to help you become a well-rounded developer
Published on: June 1, 2020This week I wanted to write about something non-technical. And while the topic of this week's post isn't a technical one, I think it's an important topic for developers who want to expand their knowledge, and deepen their skills. I have been a developer professionally for more than ten years at this point and in these ten years there are some fundamental lessons I have learned that I believe have helped me get where I am today. In this week's post, I will share five tips with you that have made into the developer I am today, and I strongly...
Read more...What’s the difference between catch and replaceError in Combine?
Published on: May 29, 2020There are several ways to handle errors in Combine. Most commonly you will either use catch or replaceError if you want to implement a mechanism that allows you to recover from an error. For example, catch is useful if you want to retry a network operation with a delay. The catch and replaceError operators look very similar at first glance. They are both executed when an error occurs in your pipeline, and they allow you to recover from an error. However, their purposes are very different. When to use catch The catch operator is used if you want to inspect...
Read more...Retrying a network request with a delay in Combine
Published on: May 25, 2020Combine comes with a handy retry operator that allows developers to retry an operation that failed. This is most typically used to retry a failed network request. As soon as the network request fails, the retry operator will resubscribe to the DataTaskPublisher, kicking off a new request hoping that the request will succeed this time. When you use retry, you can specify the number of times you want to retry the operation to avoid endlessly retrying a network request that will never succeed. While this is great in some scenarios, there are also cases where this behavior is not what...
Read more...Reclaim disk space by deleting old iOS simulators and Device Support files
Published on: May 24, 2020After using a MacBook that runs Xcode for a few years it's likely that your disk space is starting to fill up good. A large part of this disk space can be occupied by Device Support files that are used by Xcode for older iOS versions, or by iOS simulators that are no longer available on your machine. To clean these files up you can do the following: Go to your Terminal and type open ~/Library/Developer/Xcode/iOS\ DeviceSupport Delete folders for iOS versions that you no longer need to support. Do the same with open ~/Library/Developer/Xcode/watchOS\ DeviceSupport Clean up unavailable simulators...
Read more...Throttle network speeds for a specific host in Charles
Published on: May 21, 2020Sometimes you'll want to test whether your app works properly under poor networking conditions. One way to test this is Apple's Network Link Conditioner. Unfortunately, this will slow internet speeds for your entire machine to a crawl which can be counterproductive. Especially if you want to throttle your app for a longer period of time. If you have Charles installed to debug your app's network traffic, you can use it to throttle network speeds for the entire system, or for a selection of hosts which is exactly what we're looking for. To enable throttling in Charles you can either go...
Read more...