I still find it hard to write maintainable multi-threaded code for macOS or iOS [...] without having to worry about the minutiae of parallelism (threads, semaphores, locks, barriers, etc.).
I find this surprising, as GCD does insulate you from that low-level stuff. When you need to work with mutable data, just create a dispatch queue, and only ever access the data by dispatching a function to the queue. Both Swift and Objective-C have friendly syntax for anonymous functions that makes this lightweight and easy.
This reads like someone who purposely missed that part of the documentation just to write this article.
I think I would have less hang ups if the author just came out and said I wanted to try using Go for multi-threaded code with Swift instead of trying to make GCD sound so confusing.
Even so, I'm glad it's been written. I didn't know you could call Go code from C code (new feature since Go 1.5 apparently) or that Swift could access compiled files with these "module map" things. I'm really glad to see they've both evolved to include useful interoperability and play nicely with other things now, and I'm glad this article shows how in a practical way.
> Swift could access compiled files with these "module map" things
That's been the case since it was released – all Swift C / ObjC interop happens at the "module" level. You write a Clang module map (if you're not using a system library which already has one), point the map at the relevant header(s), then you can import that module into your Swift code.
That said, not everything is capable of being imported (variadics, complex macros, VLA structs, and a few others).
It seems like there's a better way to go about this than my method. I was motivated to do this not because I don't like GCD (I still don't) but because I really like Go and IPFS and didn't think that iOS should be left out of the party.
But, looking at it again, I still don't think GCD is all that great. This seems to have been intentionally designed to be different from the way so many other systems handle the issue - or at least, different enough that its still relatively discomfiting to attempt to port something away from iOS and not just to it ...
GCD is fantastic and way better than everything on macOS before it, but it's still not a silver bullet (neither is Go).
Cocoa is incredibly fragile about the main thread, so you need to be super careful what runs in what queue. If you add KVO/bindings into the mix, it needs an extraordinary level of paranoia^Wdilligence.
> Cocoa is incredibly fragile about the main thread, so you need to be super careful what runs in what queue.
This basically just boils down to "don't touch the UI off the main thread". There are some exceptions with CoreAnimation, but other than that the main thread checker will yell at you if you do something wrong.
The main thread checker is a godsend. Enabling it + TSAN + Core Data concurrency assertions have reduced the threading bugs my team deals with to almost 0.
Anywhere we're doing something specific, we make liberal use of `dispatchPrecondition(condition: .onQueue(mySerialQueue))` or `assert(Thread.isMainThread)`.
Even in places where we are doing multithreaded UI rendering (mostly CATiledLayer), it's become a non-issue. It's a night-and-day difference compared to 5+ years ago.
> Cocoa is incredibly fragile about the main thread
That basically applies to all UI framework. At least I'm not aware about one which provides good support for accessing it from another thread.
There is obviously a reason for it, which is that UI frameworks are incredibly stateful, and trying to manipulate lots of state from multiple threads at once rarely works out well.
Not sure what you mean when you say "common threading issues"; the entire point of the Be design was to force all threads to use message-based communication instead of shared memory for virtually all uses, and mandatory locking otherwise. It's extremely effective.
I found adopting a functional reactive programming really works well for multithreaded systems.
GCD is super great and I use it a lot (...if you had to extend AsyncTask recently: my condolences), but it doesn't safeguard you from race conditions, dealing with locks and all that fun stuff.
This is correct, golang doesn't give you thread safety for free. Values passed via channels are safe as they're implicitly locked (i.e. it's a blocking threadsafe queue), and there are happens-before semantics for starting a goroutine, otherwise you're on your own (explicit locks / atomics / lock-wrappers like sync.WaitGroup). E.g. variable assignment isn't even atomic.
> Give Emporter a try and let me know how it compares to an Electron app.
I don't see how this technique is comparable to Electron. The article does not describe anything related to a cross-platform user interface, which is what Electron addresses. You can write non-UI logic that is cross-platform in half a dozen mainstream languages and another dozen less popular ones. That's not a big deal. It's the UI component that is harder to achieve, and that is what Electron offers.
Thanks for your feedback. Honestly, I was just being sassy. It probably is like comparing apples to oranges. This technique does not concern itself with a cross-platform interface. It only makes it possible to keep the logic cross-platform so that the "only" difference is the UI implementation, which can be done using native SDKs.
Just wanted to say that module maps are absolutely not necessary to have static libraries (.a file author compiles his go program to) in your project.
All you need is an objective-c bridging header where you do an #import "name_of_header_.h" for every header. After that, the headers are visible to all of your swift code. It's no different than mixing objective-c and swift, except here you're mixing your language of choice, compiled to callable C functions inside a static library.
To recap - drag .h and .a files the same way you have .swift files into your xcode project. Add a BridgingHeader.h file, go to it, fill it with #import "name_of_header.h" statements. Lastly, the project needs to know you're using a bridging header, that's done in the project target's Build Settings tab, under "Objective-C Bridging Header" you need to have the value set to the filename you chose for your bridging header.
This is not unique to calling Go in Swift btw - any language that can be called from C, can be called from Objective-C, and therefore Swift. One thing to be aware of is memory management - unless you're passing things by value (copying), making sense of when things can be safely deallocated across languages is non-trivial.
Heads up: you made this error at least twice, once in the article and once in the repo. https://brians.wsu.edu/2016/05/31/complement-compliment/ It's complementary instead of complimentary. The word "complimentary" is rarely used, but could be used if you were to mention books related to complimenting people.
I don't find GCD difficult to write for, maybe if you are used to Go it is different enough. Not sure what exactly you would use Go for in an iOS/Macos app unless maybe you have a Go framework you want to include.
I consider GCD to be a bit of a smell - not because its difficult to write for, but rather because its difficult to port such code elsewhere. And really, this seems to be the only rationale for Apple having maintained it as a component of the architecture for iOS - to be relatively different, or just different enough, from other systems to dissuade porting the code elsewhere.
Lets try this a different way. Whats so great about GCD that other processor resource management systems get so wrong, that using GCD is the only thing that makes sense to you?
Right, brain fart on the train. But the point still stands, why should we use GCD when there are other more portable means out there - which are also supported on iOS - to do the same thing? It's just different for the sake of being different.
Wikipedia says GCD and Go are the same age (both released 2009). Is Go also different for the sake of being different? Why is Go good and GCD bad?
I imagine we’re not going to convince each other of the technical merits, but I personally think GCD is a very nice programming model. I’m less of a fan of Go but plenty of people love it so clearly there’s something there. There’s room for both.
It’s a valid criticism of a lot of Apple tech that it’s very locked down, but given libdispatch is open source (as are the Clang changes to support block syntax in C) I don’t think that criticism is fair here.
I found your article particularly interesting, because lately I've been experimenting with calling Swift from Go.
The approach is based on the same principle: cgo as bridge between Go and a C library. The C library is build by the Swift package manager. Blog post on Dev community with details:
Zenly ( http://zen.ly, millions of users, bought by snapchat ) has had an app coded in go and swift for ios ( and go and java for android) for a few years. Judging by the app, and the fact that it’s cross platform i’d say the go part does all the data processing part.
Thanks for sharing this! As someone who writes in Go every day but stopped following it a while back, I had no idea you could even call Go from C much less Swift.
I don't see the author pointing out any faults in Swift. There's a passing comment about GCD being complicated, which I personally disagree with, but this isn't specific to Swift.
I am go ahead and say it: Go is a replacement for Perl. It is nice for process and machine administration automation. Can you write your app in go? yes. Could you also write your app in perl cgi? sure. Did many people wrote perl cgi apps? yes. did they regret it. yes. Will people writing apps in go will regret it? who knows. Probably yes.
This comment is not about the language per-se. it is about the current goals of the people with money and weight behind the language right now. I guess the regret will come or not if those goals keep or change.
I'm in the midst of a giant Perl rewrite to Go. I see Perl as glue code, but as soon as you want a "real app" (for various definitions thereof), Perl will bite you. Go is designed for large teams and networked services with concurrency as a first class citizen.
In our Perl code base, we have had so many issues with auto-vivification, lack of argument tracing (just pass around @_ everywhere!), callback hell in AnyEvent for concurrency, and more. Maybe if you use Moose everywhere, you can get some form of sanity, but I doubt it. Engineers I have full respect for have scratched their heads trying to initially dive into this perl. What I can grant however is that it is able to do a lot of work (given enough machines!).
For the Go version, I know exact method signatures and variable types. Concurrency if first class. And just about anyone can read the code and figure out what is going on. We've onboarded new grads who can help put solid features into this already large Go codebase quickly. We are seeing 20x optimization in throughput over one code base from Perl (it requires a lot of waiting on remote servers we don't control), and 100x in another.
I can't imagine regretting the choice to write in Go for networked services running in the backend.
Go is designed for armies of simple minded developers, Java 1.0 revisited.
“The key point here is our programmers are Googlers, they’re not researchers. They’re typically, fairly young, fresh out of school, probably learned Java, maybe learned C or C++, probably learned Python. They’re not capable of understanding a brilliant language but we want to use them to build good software. So, the language that we give them has to be easy for them to understand and easy to adopt.”
Simple minded is your words, not his (in another talk, I think he refers to these people as regular developers).
The harsh language there is not being "capable of understanding a brilliant language." I'm not aware of what was meant by a brilliant language, but I have to assume that it means research languages. I would not expect fresh grads to build production worthy code in any language, but especially not in a research languages (which typically are relegated to the realm of research because they are not capable of being used by large teams for the making of good software).
I'm in the business of creating value with good software, not ivory tower building that only a smaller proportion of software artisans can build and maintain.
The crowing achievements of AI that interact with people are Alexa et al. Alexa can't even tell me what the newest releases are for movies.
When we can talk to an AI and describe something novel and it can interpret that, then I'll start to worry about software development jobs being lost to AI.
Didn't that scare happen a while ago with everyone worried about India taking all the software jobs? Didn't really happen. Could happen again, sure. I don't think more advanced AI or more simple programming languages will be a large factor in that.
They couldn't be further from each other. Just from browsing code, they are on opposite ends of the spectrum. Go is easily readable, but at the cost of brevity. Perl is -typically- unreadable, with a lot of one liner magic.
If you are stating merely can a person write a web app in both, well, I think that goes for any language in existence.
Perl could've been readable - nothing in the language prevents you from writing readable code. The problem was two-fold, imho:
1) regex was treated like the primary way to do things - even when it wasn't necessarily called for - at the expense of readability (and Perl supported it so well)
2) sysadmins
The two combined together (and possibly the fact that it was the early days of commercial internet service) led to the idea that anything done in Perl was destined to look like "line noise" to actual SWEs.
I know. Perl was all that at the time. daunting! modern! no compiler OMG! text transformation on the code itself! it was the bee knees.
All that Go delivers today, over the medium, is very similar to what perl also delivered yesteryear, on top of yesteryear medium. But also like perl, it is being born from a bunch of old unix system engineers :)
I find this surprising, as GCD does insulate you from that low-level stuff. When you need to work with mutable data, just create a dispatch queue, and only ever access the data by dispatching a function to the queue. Both Swift and Objective-C have friendly syntax for anonymous functions that makes this lightweight and easy.