-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
135 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
Pod::Spec.new do |s| | ||
s.name = "HUIGCDDispatchAsync" | ||
s.version = "1.0.0" | ||
s.summary = "A simple wrapper around GCD dispatch_async() function to make easily cancellation." | ||
s.homepage = "https://github.com/Tinghui/HUIGCDDispatchAsync" | ||
s.license = "MIT" | ||
s.author = { "Tinghui" => "tinghui.zhang3@gmail.com" } | ||
s.platform = :ios, "6.0" | ||
s.source = { :git => "https://github.com/Tinghui/HUIGCDDispatchAsync.git", :tag => s.version } | ||
s.source_files = "HUIGCDDispatchAsync/HUIGCDDispatchAsync.{h,m}" | ||
s.requires_arc = true | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,83 +1,89 @@ | ||
HUIGCDDispatchAsync | ||
=================== | ||
##HUIGCDDispatchAsync | ||
|
||
|
||
A simple wrapper around GCD dispatch_async() function to make easily cancellation. | ||
|
||
## Demo | ||
Please check the Demo target in `HUIGCDDispatchAsync.xcodeproj` for more detail. | ||
|
||
<p><strong>How to use:<strong></p> | ||
<ul><li>0. Just Drag and Add HUIGCDDispatchAsync.h/m files into your project, and import "HUIGCDDispatchAsync.h"</li></ul> | ||
<ul><li>1. Dispatch an async block with 'dispatch_async_HUI' function.</li></ul> | ||
<ul><li>2. Obtain the returned 'HUIBlockFlag' object.</li></ul> | ||
<ul><li>3. Set the 'HUIBlockFlag' object's cancel property to YES if you want cancel the previous dispatched block.</li></ul> | ||
<ul><li>4. Within your actual block code, it's your responsibility to check and cancel the block.</li></ul> | ||
|
||
Or Example: | ||
|
||
```objc | ||
/* | ||
* Dispatch an async block with 'dispatch_async_HUI' function | ||
* Keep the returned 'HUIBlockFlag' object if you need cancel the block later, Otherwise, just leave it away. | ||
*/ | ||
HUIBlockFlag *blockFlag = dispatch_async_HUI(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(HUIBlockFlag *flag){ | ||
|
||
See the demo for example: | ||
|
||
/* | ||
* Dispatch an async block with 'dispatch_async_HUI' function | ||
* Obtain the returned 'HUIBlockFlag' object if you need cancel the block later, Otherwise, just leave it away. | ||
* Use while(YES) to simulate an endless async operation. | ||
*/ | ||
HUIBlockFlag *blockFlag = dispatch_async_HUI(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(HUIBlockFlag *flag){ | ||
while (YES) | ||
{ | ||
/* | ||
* while(YES) is just used to simulate an endless async operation. | ||
* Inside your actual block code, use the 'flag' parameter to check whether need to cancel current block operation. | ||
* If need cancel, you should clean up and return. | ||
*/ | ||
while (YES) | ||
if (flag.isCancelled) | ||
{ | ||
/* | ||
* Within your actual block code, use the 'flag' parameter to check whether need cancel this block | ||
* If need cancel, you should clean up and cancel it. | ||
*/ | ||
if (flag.isCancelled) | ||
{ | ||
goto CancelBlock; | ||
} | ||
[NSThread sleepForTimeInterval:1.0]; | ||
NSLog(@"%@ Simulator: Job step costs 1 second ", [NSDate date]); | ||
/* | ||
* You can increase frequency of the checking operation, to make this block stops ASAP. | ||
*/ | ||
if (flag.isCancelled) | ||
{ | ||
goto CancelBlock; | ||
} | ||
[NSThread sleepForTimeInterval:3.0]; | ||
NSLog(@"%@ Simulator: Job step costs 3 second ", [NSDate date]); | ||
if (flag.isCancelled) | ||
{ | ||
goto CancelBlock; | ||
} | ||
[NSThread sleepForTimeInterval:0.5]; | ||
NSLog(@"%@ Simulator: Job step costs 0.5 second ", [NSDate date]); | ||
goto CancelBlock; | ||
} | ||
/* | ||
* If you keep a high frequency of the cancel checking operation. A GOTO label is helpful. | ||
[NSThread sleepForTimeInterval:1.0]; | ||
NSLog(@"%@ Simulator: Job step costs 1 second ", [NSDate date]); | ||
/* | ||
* You can increase frequency of the checking operation, to make current block stops ASAP. | ||
*/ | ||
CancelBlock: | ||
//Do some clean up operations here | ||
NSLog(@"Block is cancelled"); | ||
return; | ||
if (flag.isCancelled) | ||
{ | ||
goto CancelBlock; | ||
} | ||
[NSThread sleepForTimeInterval:3.0]; | ||
NSLog(@"%@ Simulator: Job step costs 3 seconds ", [NSDate date]); | ||
}); | ||
|
||
|
||
if (flag.isCancelled) | ||
{ | ||
goto CancelBlock; | ||
} | ||
[NSThread sleepForTimeInterval:0.5]; | ||
NSLog(@"%@ Simulator: Job step costs 0.5 seconds ", [NSDate date]); | ||
} | ||
|
||
/* When you want to cancel the async block. Just set the returned 'HUIBlockFlag' object's cancel to YES | ||
* The following code simulates 'cancel the block after 20 seconds' | ||
/* | ||
* If you keep a high frequency of the cancel checking operation. A GOTO label is helpful. | ||
*/ | ||
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){ | ||
[NSThread sleepForTimeInterval:20.00]; | ||
blockFlag.cancel = YES; | ||
NSLog(@"BlockFlag is set to cancelled"); | ||
}); | ||
CancelBlock: | ||
NSLog(@"Clean up and return, Block is cancelled"); | ||
return; | ||
|
||
}); | ||
|
||
|
||
|
||
/* When you want to cancel the async block. Just set the returned 'HUIBlockFlag' object's cancel to YES | ||
* The following codes simulate 'cancel the block after 20 seconds' | ||
*/ | ||
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){ | ||
[NSThread sleepForTimeInterval:20.00]; | ||
blockFlag.cancel = YES; | ||
NSLog(@"BlockFlag is set to cancelled"); | ||
}); | ||
``` | ||
## Installation by CocoaPods | ||
1. Add pod into your Podfile | ||
```objc | ||
pod 'HUIGCDDispatchAsync', :git => 'https://github.com/Tinghui/HUIGCDDispatchAsync.git', :tag => '1.0.0' | ||
``` | ||
|
||
2. Run `pod install` | ||
3. `#import <HUIGCDDispatchAsync/HUIGCDDispatchAsync.h>` | ||
|