Why Alibaba’s coobjc Coroutine Framework Is Revolutionizing iOS Asynchronous Programming
Alibaba’s newly open‑sourced coobjc framework brings coroutine‑based async/await, generators, and actor model support to Objective‑C and Swift on iOS, offering cleaner code, better performance, and reduced threading issues compared to traditional block‑based GCD approaches, with detailed design, implementation, and benchmark insights.
Introduction
Alibaba has officially open‑sourced the coobjc coroutine development framework under the Apache 2.0 license. Developers can download it from GitHub.
coobjc is an open‑source coroutine framework for iOS that supports Objective‑C and Swift, and provides the cokit library to coroutine‑ify parts of Foundation and UIKit.
iOS Asynchronous Programming Problems
Since the first iOS release in 2008, block‑based asynchronous programming using GCD has been the dominant approach, but it suffers from many drawbacks:
Nested “callback hell”
Complex and verbose error handling
Forgetting to call completion handlers
Difficult conditional execution
Hard to combine results from independent calls
Executing UI code on background threads
Hard‑to‑diagnose multithread crashes
Lock and semaphore misuse causing stalls
Even with strict coding standards and training, multithread‑related crashes remain a serious issue.
Solution
Coroutines are the standard solution in many languages (C#, Kotlin, Python, JavaScript). Since C++ added coroutine support in 2017 and Swift 5 includes coroutine syntax, a coroutine‑based async model is the natural evolution. However, Objective‑C lacks native coroutine support, so Alibaba’s Taobao architecture team built coobjc using assembly and C to provide coroutine capabilities for both Objective‑C and Swift.
Core Capabilities
Async/Await‑style programming: await returns the result of an asynchronous call synchronously.
Generator support similar to Kotlin for lazy sequence generation.
Actor Model implementation for thread‑safe modules.
Tuple support enabling multiple return values in Objective‑C.
Built‑in System Extension Libraries
Coroutine‑ified extensions for container classes such as NSArray and NSDictionary.
Extensions for data objects like NSData, NSString, UIImage.
Network extensions for NSURLConnection and NSURLSession.
Parsing extensions for NSKeyedArchiver and NSJSONSerialization.
Architecture
Kernel layer: manages stack switching, coroutine scheduler, and channel communication.
Middle layer: provides coroutine‑based operators (async/await, generator, actor).
Top layer: coroutine‑ified extensions covering most Foundation and UIKit I/O methods.
Implementation Principles
Coroutines rely on two primitive operations: Yield (suspend execution) and Resume (continue execution). By saving the current call‑stack state and restoring it later, coobjc achieves these operations.
Several implementation techniques exist (glibc ucontext, assembly context switching, protothreads, setjmp/longjmp, compiler sugar). coobjc uses a custom assembly‑based context switch that mimics ucontext, supporting armv7, arm64, i386, and x86_64 on real devices and simulators.
Code Example
The following image shows a simple network request before and after conversion to coobjc coroutine style, reducing the code size by about 50% and improving readability.
<img src="https://mmbiz.qpic.cn/mmbiz_jpg/LwZPmXjm4Wz1aY0r2opelOby95bQzUUNfuyich2u4al2p2V0LLMdX91Yr4QX18x1kA5M8sLClm4wGJL7rh0ZF8Q/640"/> <img src="https://mmbiz.qpic.cn/mmbiz_jpg/LwZPmXjm4Wz1aY0r2opelOby95bQzUUNe0zv4DsToJticjxwn1RJInLibmS9HbBBAxLviaogykkPD2dMqBL2XHcFw/640"/>Performance Evaluation
Benchmarks on an iPhone 7 (iOS 11.4.1) comparing coobjc with traditional multithreading show that for low concurrency the difference is small, but as concurrent tasks exceed 1 000, traditional threads suffer from scheduling failures while coobjc continues to run smoothly.
In a real‑world scenario within the Taobao app, applying coobjc to performance‑critical pages reduced main‑thread I/O and parsing stalls, yielding roughly a 20 % frame‑rate improvement on low‑end devices (iPhone 6 and below).
Advantages
Simplicity : Only a few operators, far fewer than reactive libraries.
Ease of Use : Fewer APIs than GCD, minimal code changes required for coroutine conversion.
Clarity : Write asynchronous logic in a synchronous, sequential style, reducing bugs and improving readability.
Performance : Faster scheduling without kernel‑level thread switches, and reduced lock/ semaphore contention.
“Programs are written for people to read, not just for machines to execute.” – Abelson & Sussman
The coroutine programming paradigm enables developers to write more elegant, robust, and readable concurrent code, decreasing reliance on threads and locks while boosting performance and stability.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Alibaba Cloud Developer
Alibaba's official tech channel, featuring all of its technology innovations.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
