A super slim solution to the nested asynchronous computations

Related tags

WebSocket Me
Overview

Me

A super slim and in-place solution to the nested asynchronous computations problem

The mindset of software developers is changing, async programming represents 70% of the code in a cloud-based app and nested closures/blocks are a bad idea most of the time in terms of maintainability, readability and control (confusion about multiple vars with the same name in the same scope).

In order to deal with it, we wrote Me, a super slim piece of software (less than 200 lines) that acts like a magic by chaining your code instead of nesting it.

Example

Old method

MyAPI.login {
	//Do your stuff and then request posts...
	MyAPI.posts {
		//Do your stuff and then request comments...
		MyAPI.comments {
			//Do your stuff and then request likes...
			MyAPI.likes {
				//We are done here
			}
		}
	}
}

Me method

Me.start { (me) in
	MyAPI.login {
		//Do your stuff and then request posts...
		me.runNext()
	}
}.next { (caller, me) in
	MyAPI.posts {
		//Do your stuff and then request comments...
		me.runNext()
	}
}.next { (caller, me) in
	MyAPI.comments {
		//Do your stuff and then request likes...
		me.runNext()
	}
}.next { (caller, me) in
	MyAPI.likes {
		//We are done here
		me.end()
	}
}.run()

As you can see, the 'shifting' has been solved and the developer has the full control of the code's flow.

So, what the heck is the Me object??

The Me object is a proxy, an holder of the current block and the next in the chain.

  • Me.start command, here you can add your first block, and then continue with:

  • .next command, as parameters there are two Me objects, the first one refers to the Me caller, and the other refers to the Me object that holds the current block that you are working on.

  • .run() command, start the first block, you must add it at the end of the chain.

Inside each block, you must add an instruction for the next block that should be executed, this is accomplished with runNext() or end()

  • me.runNext() is the command used to call the next block, it should be called when your async call is returned and you are ready to run the next block.

  • me.end() expresses the intention to end the chaining and release all the Me objects and the blocks associated to it.

N.B. If you don't call runNext(), the next block will not be executed

N.B. If you don't call end(), nothing will be released

Pros

  • Easy to read
  • Easy to maintain
  • Me doesn't ask you to write your own proxy (like Promise), you can just refactor your nested blocks and split them in different .next blocks.
  • Me is built on top of the Grand Central Dispatcher, it's safe and allows you to run the code in your own queue
  • Allows you to jump to a particular block and check the name of the caller
  • Allows you to set all of your parameters in the parameters dictionary

Cons

  • The code will be few lines longer (2/3 for each block) compared to the nested code
  • You can't forget to add runNext() or end() at the end of the execution, otherwise the next block will not be executed or the memory will not be released
  • You can't pass parameters directly as block parameters, but you must specify them into the parameters dictionary

Contacts

We would love to know if you are using Me in your app, send an email to [email protected]

You might also like...
Promises simplify asynchronous programming, freeing you up to focus on the more important things
Promises simplify asynchronous programming, freeing you up to focus on the more important things

Promises simplify asynchronous programming, freeing you up to focus on the more important things. They are easy to learn, easy to master and result in

Syntactic sugar in Swift for asynchronous dispatches in Grand Central Dispatch

Async Now more than syntactic sugar for asynchronous dispatches in Grand Central Dispatch (GCD) in Swift Async sugar looks like this: Async.userInitia

AwaitKit is a powerful Swift library which provides a powerful way to write asynchronous code in a sequential manner.
AwaitKit is a powerful Swift library which provides a powerful way to write asynchronous code in a sequential manner.

AwaitKit is a powerful Swift library inspired by the Async/Await specification in ES8 (ECMAScript 2017) which provides a powerful way to write asynchronous code in a sequential manner.

🌀 Swift Combine extensions for asynchronous CloudKit record processing

Swift Combine extensions for asynchronous CloudKit record processing. Designed for simplicity.

Asynchronous image downloader with cache support as a UIImageView category
Asynchronous image downloader with cache support as a UIImageView category

This library provides an async image downloader with cache support. For convenience, we added categories for UI elements like UIImageView, UIButton, M

Simple asynchronous HTTP networking class for Swift

YYHRequest YYHRequest is a simple and lightweight class for loading asynchronous HTTP requests in Swift. Built on NSURLConnection and NSOperationQueue

Syntactic sugar in Swift for asynchronous dispatches in Grand Central Dispatch (iOS7+ and OS X 10.9+ compatible)

Async.legacy Syntactic sugar in Swift for asynchronous dispatches in Grand Central Dispatch (GCD) Async rewritten for iOS7 and OS X 10.9 Compatibility

AsyncTimer is a precision asynchronous timer. You can also use it as a countdown timer
AsyncTimer is a precision asynchronous timer. You can also use it as a countdown timer

AsyncTimer 🌟 Features Can work as a countdown timer Can work as a periodic Timer Can work as a scheduled timer Working with user events (like: scroll

A pure Swift high-performance asynchronous image loading framework. SwiftUI supported.
A pure Swift high-performance asynchronous image loading framework. SwiftUI supported.

Longinus Longinus is a pure-Swift high-performance asynchronous web image loading,caching,editing framework. It was learned from Objective-C web image

Write great asynchronous code in Swift using futures and promises

BrightFutures How do you leverage the power of Swift to write great asynchronous code? BrightFutures is our answer. BrightFutures implements proven fu

Asynchronous socket networking library for Mac and iOS

CocoaAsyncSocket CocoaAsyncSocket provides easy-to-use and powerful asynchronous socket libraries for macOS, iOS, and tvOS. The classes are described

Pigeon is a SwiftUI and UIKit library that relies on Combine to deal with asynchronous data.

Pigeon 🐦 Introduction Pigeon is a SwiftUI and UIKit library that relies on Combine to deal with asynchronous data. It is heavily inspired by React Qu

Syntactic sugar in Swift for asynchronous dispatches in Grand Central Dispatch

Async Now more than syntactic sugar for asynchronous dispatches in Grand Central Dispatch (GCD) in Swift Async sugar looks like this: Async.userInitia

iOS utility classes for asynchronous rendering and display.

YYAsyncLayer iOS utility classes for asynchronous rendering and display. (It was used by YYText) Simple Usage @interface YYLabel : UIView @property NS

Enables easy, convenient asynchronous asset loading in RealityKit for many different kinds of assets.

RealityKit Asset Loading Discussion This package includes classes and examples that enable easy, convenient asynchronous asset loading in RealityKit f

Asynchronous image loading framework.
Asynchronous image loading framework.

YYWebImage YYWebImage is an asynchronous image loading framework (a component of YYKit). It was created as an improved replacement for SDWebImage, PIN

A modern download manager based on NSURLSession to deal with asynchronous downloading, management and persistence of multiple files.
A modern download manager based on NSURLSession to deal with asynchronous downloading, management and persistence of multiple files.

TWRDownloadManager TWRDownloadManager A modern download manager for iOS (Objective C) based on NSURLSession to deal with asynchronous downloading, man

MailCore 2 provide a simple and asynchronous API to work with e-mail protocols IMAP, POP and SMTP.

MailCore 2: Introduction MailCore 2 provides a simple and asynchronous Objective-C API to work with the e-mail protocols IMAP, POP and SMTP. The API h

Swift extensions for asynchronous CloudKit record processing

⛅️ AsyncCloudKit Swift extensions for asynchronous CloudKit record processing. D

Comments
  • Can I call runNext() multiple times?

    Can I call runNext() multiple times?

    I was wandering if the library was thought to being able to run the next block multiple times. And if yes, when one should call the end() function (if the last block is called multiple times)?

    opened by sferrini 4
  • Installation section

    Installation section

    Hey, your library is really interesting.

    The only problem I found was the README.md, which lacks an Installation Section I created this iOS Open source Readme Template so you can take a look on how to easily create an Installation Section If you want, I can help you to organize the lib.

    What are your thoughts? πŸ˜„

    opened by lfarah 1
  • Unit tests

    Unit tests

    This piece of software seems to be a perfect candidate to be covered with unit-tests. It would greatly simplify contributing!

    One of the most popular frameworks for unit-testing Obj-C and Swift code (probably de-facto standard) these days is Quick & Nimble.

    What do you think?

    opened by werediver 0
Owner
Pasquale Ambrosini
Mobile software developer. πŸ„β€β™‚οΈπŸ›ΉπŸ»πŸš€
Pasquale Ambrosini
Multi-tier UIScrollView nested scrolling solution. πŸ˜‹πŸ˜‹πŸ˜‹

Multi-tier UIScrollView nested scrolling solution. Snapshots Requirements iOS 9.0+ Xcode 10.0+ Swift 4.2+ Installation CocoaPods CocoaPods is a depend

Jiar 1.2k Dec 30, 2022
A slim implementation of a websocket server using Swift and Vapor 4.0.

Swift Websocket Server Example using Vapor 4.0 This project includes a minimum working example for a websocket server written in Swift. To interact wi

Adrian Hupka 5 Sep 22, 2022
A UICollectionViewLayout subclass displays its items as rows of items similar to the App Store Feature tab without a nested UITableView/UICollectionView hack.

CollectionViewShelfLayout A UICollectionViewLayout subclass displays its items as rows of items similar to the App Store Feature tab without a nested

Pitiphong Phongpattranont 374 Oct 22, 2022
Turning on a VPN is always a painful experience on an iOS device due to the deep nested menus.

VPN On Turning on a VPN is always a painful experience on an iOS device due to the deep nested menus. This App installs a Today Widget into Notificati

Lex Tang 4.4k Dec 26, 2022
Holistically-Nested Edge Detection (HED) using CoreML and Swift

HED-CoreML Holistically-Nested Edge Detection (HED) using CoreML and Swift This is the repo for tutorial, that contains an example application that ru

Andrey Volodin 101 Dec 25, 2022
`Republished` is a property wrapper enabling nested ObservableObjects in SwiftUI.

Republished The @Republished proprty wrapper allows an ObservableObject nested within another ObservableObject to naturally notify SwiftUI of changes.

Adam Zethraeus 13 Dec 5, 2022
Encode and decode deeply-nested data into flat Swift objects

DeepCodable: Encode and decode deeply-nested data into flat Swift objects Have you ever gotten a response from an API that looked like this and wanted

Mike Lewis 91 Dec 26, 2022
Futures is a cross-platform framework for simplifying asynchronous programming, written in Swift.

Futures Futures is a cross-platform framework for simplifying asynchronous programming, written in Swift. It's lightweight, fast, and easy to understa

David Ask 60 Aug 11, 2022
GroupWork is an easy to use Swift framework that helps you orchestrate your concurrent, asynchronous functions in a clean and organized way

GroupWork is an easy to use Swift framework that helps you orchestrate your concurrent, asynchronous functions in a clean and organized way. This help

Quan Vo 42 Oct 5, 2022
Write great asynchronous code in Swift using futures and promises

BrightFutures How do you leverage the power of Swift to write great asynchronous code? BrightFutures is our answer. BrightFutures implements proven fu

Thomas Visser 1.9k Dec 20, 2022