Skip to content
This repository has been archived by the owner on May 19, 2018. It is now read-only.
/ RXSynchronously Public archive

A very convenient way to wait for asynchronous work to complete.

License

Notifications You must be signed in to change notification settings

robrix/RXSynchronously

Repository files navigation

#RXSynchronously

A convenient way to synchronize the calling thread on some asynchronous work.

// 1
RXSynchronously(^(RXSynchronousCompletionBlock didComplete) {
	// 2
	[worker doAsynchronousWorkWithCompletionHandler:^{
		// 3
		didComplete();
	}];
});
// 4

Control flow is, as you would expect, effectively serial:

  1. Control enters.
  2. Some asynchronous work is begun.
  3. The asynchronous work completes, at which point we call the didComplete block that RXSynchronously passed us.
  4. The calling thread resumes.

#Use cases

I use this widely when unit testing asynchronous code, with basically the same pattern as in the example above. OCUnit tests are necessarily synchronous, so RXSynchronously is an excellent way to button things down.

#Gotchas

  • As with any attempt to block until some signal is received, you should beware of deadlocks. RXSynchronouslyWithTimeout may be more appropriate in some cases. This is especially likely when unit testing asynchronous code that executes on the main queue with OCUnit, as OCUnit executes on the main thread (where the main queue is dispatched).
  • Absolutely do not forget to call the didComplete block.

About

A very convenient way to wait for asynchronous work to complete.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published