Coobjc Open Sourcced: Alibaba’s Open Source Coroutine Framework for iOS Developers

Offering full coroutine support for iOS languages

Image for post
Image for post
Image for post
Image for post

The Problem of Async Programming

Image for post
Image for post

All About Coobjc: From Core Features to Source Code

System architecture

Image for post
Image for post

Operating principles

Example code

//Asynchronously loading data from the network
[NSURLSession sharedSession].configuration.requestCachePolicy = NSURLRequestReloadIgnoringCacheData;
NSURLSessionDownloadTask *task = [[NSURLSession sharedSession] downloadTaskWithURL:url completionHandler:
^(NSURL *location, NSURLResponse *response, NSError *error) {
if (error) {
return;
}

//Parsing data in child threads and generating images
dispatch_async(dispatch_get_global_queue(0, 0), ^{
NSData *data = [[NSData alloc] initWithContentsOfURL:location];
UIImage *image = [[UIImage alloc] initWithData:data];
dispatch_async(dispatch_get_main_queue(), ^{
//Dispatch to the main thread to view the image
imageView.image = image;
});
});

}];
//Main thread creates coroutine
co_launch(^{
//Asynchronous network data loading
NSData *data = await([NSURLConnection async_sendAsynchronousRequest:request response:nil error:nil]);
//Asynchronous image parsing
UIImage *image = await([UIImage async_imageWithData:data]);
//Show image
imageView.image = image;
});

Performance

Image for post
Image for post

In a Nutshell

Concision

Ease of use

Clarity

Performance

A final thought

— Abelson and Sussman

Alibaba Tech

First-hand & in-depth information about Alibaba's tech innovation in Artificial Intelligence, Big Data & Computer Engineering. Follow us on Facebook!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store