This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

Merge commit '3b73277db6b6a74bc88aa594d9847cadeaa07974' as 'External/…

…RXSynchronously'
  • Loading branch information...
robrix committed Nov 28, 2011
2 parents 1b60406 + 3b73277 commit 84fcd6f31664af75fe49635f08419c506017dcd3
@@ -0,0 +1,5 @@
+.DS_Store
+xcuserdata
+*.mode*
+*.pbxuser
+*.xcuserdatad
@@ -0,0 +1,7 @@
+Copyright (c) 2011 Rob Rix
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,29 @@
+#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.
Oops, something went wrong.

0 comments on commit 84fcd6f

Please sign in to comment.