Sometimes you come across code like this: Array(Set(array)).sort(by:)
, where array
is the result of a fetch from CoreData. These operations might be scattered across different files, but in essence it is exactly that. The reason the author of that code left the hash set (the Set
) is to remove duplicates, to “de-dupe”. CoreData lacks the DISTINCT
keyword from SQL even though it uses SQLite under the hood. So, you cannot ask for unique elements when fetching. This leaves you with two options: either do the work when you are reading or do the work when you are writing. That is, either filter in memory post fetch or prevent duplicates on insert.
Deadlock in GCD
The term ‘structured concurrency’ did not originate with Swift or on iOS. (See this wikipedia article for some history.) But it has become prominent in Swift. Here and here. One reason that Swift has developed this ‘structured concurrency’ syntax is, I gather, to help eliminate programming errors that have to do with concurrency and which can be difficult to debug once they are buried in thousands of lines of code in a large codebase. One such problem is deadlock. Typically, deadlock involves two concurrent tasks both of which might be waiting on the other to complete. (Perhaps like so, where there are at least two characters: the hungry duck and the restaurant manager.) But, in a philosophically trippy way, could you be deadlocked with your own self?! Indeed you could.
One-to-many delegates
There is this belief in the iOS community, perhaps reinforced by the popularity of this as an interview question, that for one-to-one callbacks we should use a (weak) delegate and for one-to-many we should use NotificationCenter. This is false.
POST using Alamofire 5
Swift 5 with Alamofire 5:
// `POST /yourendpoint`
session.request(
"https://yourbackend.com/yourendpoint",
method: .post,
parameters: payload, // `payload` is an Encodable struct
encoder: JSONParameterEncoder.default)
That’s how, in a nutshell, you would POST the contents of payload
as JSON to the server using Alamofire 5 (beta 7).
View models
A problem I often see in production code is the wide gap between the data model the service returns to the client and the model the client’s view actually needs. In other words, what the service returns is not what the client needs to display to the user, even if the service returns data from which this information can be inferred. A view model is one way to address this problem—whatever other good having a view model does, like having smarter code coverage and leaving your view’s attention focused on rendering UI.
Enums and JSON decoders
In this blog post, I will show how to have your cake and eat it too: how to use an enum in your model and yet avoid having your JSON decoder choke on unexpected values in a json. And no, I do not think the new “@unknown” keyword in Swift 5 is helpful. But luckily, the Swift 4 solution also works in Swift 5.
One applón, two apploós, five apples. Localizing plurals!
Decoding nested keys
Map-reduce on iOS, part 2
Map-reduce on iOS
There is MapReduce and then there is map-reduce. The latter (map-reduce) is a design pattern that came out of a more specific use case than perhaps most devs realize: the problem of efficiently crunching large datasets by parallelizing the work using a bunch of computers strung together in a cluster. Jeffrey Dean and Sanjay Ghemawat described this use case and the original MapReduce ‘programming model’ in their 2004 Google whitepaper … Today, map-reduce is a more general design pattern of which MapReduce can be thought of as a more specific case.
Market volatility
If engineering were like finance, I would have one colleague telling me, “the probability of an app crash is 91%” while another colleague is telling me, “it is impossible to predict an app crash with any probability higher than 40%.”
And yet the very next day (on January 5th) after the shiny job report (January 4th) that the government issued in the middle of its shutdown, it was The Economist who, blaming it on a JP Morgan model, forecast that “the probability of a recession in America in 2019 is as high as 91%”. Thanks The Economist; 91% sounds very precise. According to this Vanguard study, on the other hand, it is not possible to accurately predict where the market will go a year out. Even “P/E ratios have ‘explained’ only about 40% of the time variation in net-of-inflation returns” the Vanguard study writes.
Noncompetes
An old friend—let’s call him Finch—once told me that whenever he nicely asked any former employer if he could go work for another software firm despite having a non-compete clause in his employment contract, any former employer would always say, “Yeah, don’t worry about.” People are nice, my friend said. This was definitely true of Finch. Finch always saw the best in people.
Making a network call in Swift using Alamofire 5
You want to make the following http request to find out what the weather is right now at the visitor center in Sabino Canyon in Tucson, Arizona:
GET https://api.darksky.net/forecast/yourApiKey/32.31,-110.822
Because DarkSky is freaky accurate. Two ways to do this in Swift are: (1) using URLSession, formerly known as NSURLSession, and (2) using Alamofire 5, still in beta as of this writing and quite a bit different from Alamofire 4. In this article, I will show a GET using both: URLSession and Alamofire 5. This side-by-side comparison is handy because the Alamofire 5 migration guide has not yet been written and the Alamofire documentation is currently (as of this writing) out of date during the Alamofire 5 beta process.
Agile and art
As a writer in residence at Ox-Bow, and later at Jentel, an author of literary short fiction, Rose Lambert-Sluder, decided to apply Agile Scrum to her writing. (Agile is a set of value-driven processes for software development and there are two ways to do Agile: Scrum and Kanban.) At Ox-Bow, Ms. Lambert-Sluder was working on three different short stories…
Squashing commits
git commit --amend
Scenario. You saved your work by “doing a commit”, but then you realized you would like to make changes to it.
Solution. Amend your most recent commit!
As far as git is concerned, a commit is composed of your code plus a commit message. So, when editing your last commit, you can edit either one: the source code or the commit message.
Using a machine-learning model in your iOS app
On and off, thinking of it as formal epistemology sometimes cognitively informed (and I dig epistemology and cognitively informed stuff), I've been following machine learning since the early 2000s ever since my friend Kat got seriously into it. And so, I wanted to avoid talking about machine learning on this blog because of how much of a buzzword it has suddenly become. From Apple's WWDC announcements, I got the impression that there is now machine learning in my coffee. But iOS 11 is scheduled for release tomorrow, September 19, 2017, so I guess it's time to talk about it.
So, earlier this month while back in New York and as an appetizer for try! Swift, a professional community conference organized by Natasha the Robot (Natasha Murashev in real life), I sat in on Meghan Kane's wonderful workshop. Taking Meghan's git repo and filling in a few things, I tried out a few freely available machine learning (ML) models in an iOS Simulator.
CIDR notation: how 28 is bigger than 29
APIs: When POSTs succeed but do not return a 201
Design Patterns. Sometimes computers talk to one another. Fairly often in fact. To help make these computer conversations meaningful, engineers rely on conventions. One of these conventions is the HTTP status codes that services return to their clients. REST APIs follow these HTTP conventions, or at least should. This article is about a specific and interesting case of that: the case when everything goes according to plan.
Thinking ahead by looking back: Swift 4 @ WWDC
Now that we are probably halfway between the Apple Worldwide Developers Conference (WWDC)—this past June—and the upcoming Swift 4 migrations in prod once Xcode 9 comes out of beta, I thought it time to think ahead and revisit June. … [M]y first stop was the "What's New in Swift" talk … To whet your appetite, here are five of the many interesting things from the talk.