Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

A simple wrapper around GCD dispatch_async() function to make easily cancellation.

NotificationsYou must be signed in to change notification settings

Tinghui/HUIGCDDispatchAsync

Repository files navigation

##HUIGCDDispatchAsync

A simple wrapper around GCD dispatch_async() function to make easily cancellation.

Please check the Demo target inHUIGCDDispatchAsync.xcodeproj for more detail.

Or Example:

/* *  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){/*     *  Use while(YES) to simulate an endless async operation.*/while (YES)    {/*         *  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.*/if (flag.isCancelled)        {goto CancelBlock;        }                        [NSThreadsleepForTimeInterval:1.0];NSLog(@"%@ Simulator: Job step costs 1 second", [NSDatedate]);/*         *  You can increase frequency of the checking operation, to make current block stops ASAP.*/if (flag.isCancelled)        {goto CancelBlock;        }        [NSThreadsleepForTimeInterval:3.0];NSLog(@"%@ Simulator: Job step costs 3 seconds", [NSDatedate]);if (flag.isCancelled)        {goto CancelBlock;        }        [NSThreadsleepForTimeInterval:0.5];NSLog(@"%@ Simulator: Job step costs 0.5 seconds", [NSDatedate]);            }/*     *  If you keep a high frequency of the cancel checking operation. A GOTO label is helpful.*/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){    [NSThreadsleepForTimeInterval:20.00];    blockFlag.cancel =YES;NSLog(@"BlockFlag is set to cancelled");});

Installation by CocoaPods

  1. Add pod into your Podfile

    pod'HUIGCDDispatchAsync', :git =>'https://github.com/Tinghui/HUIGCDDispatchAsync.git', :tag =>'1.0.0'
  2. Runpod install

  3. #import <HUIGCDDispatchAsync/HUIGCDDispatchAsync.h>

About

A simple wrapper around GCD dispatch_async() function to make easily cancellation.

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp