Skip to content

Instantly share code, notes, and snippets.

@dmitryshliugaev
Last active October 23, 2016 15:58
Show Gist options
  • Save dmitryshliugaev/86ccd5bdc022908010b941eb4c88d6d9 to your computer and use it in GitHub Desktop.
Save dmitryshliugaev/86ccd5bdc022908010b941eb4c88d6d9 to your computer and use it in GitHub Desktop.
int multiplier = 7;
int (^myBlock)(int) = ^(int num) {
return num * multiplier;
};
multiplier = 8;
NSLog( @"%d", myBlock( 3 ) );
----------------------------------------------------
@interface ViewController ()
@property (atomic,strong) NSMutableArray *array;
@property NSOperationQueue *queue;
@property dispatch_semaphore_t semaphore;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.array = [[NSMutableArray alloc] init];
self.queue = [[NSOperationQueue alloc] init];
self.semaphore = dispatch_semaphore_create(1);
NSOperation *testOperation1 = [[NSInvocationOperation alloc]
initWithTarget:self
selector:@selector(test1)
object:nil];
NSOperation *testOperation2 = [[NSInvocationOperation alloc]
initWithTarget:self
selector:@selector(test2)
object:nil];
[self.queue addOperation:testOperation1];
[self.queue addOperation:testOperation2];
}
-(void)test1 {
[self.array addObject:@"test 1"];
dispatch_semaphore_wait(self.semaphore, DISPATCH_TIME_FOREVER);
[self.array addObject:@"test 4"];
NSLog(@"1 %@",self.array);
dispatch_semaphore_signal(self.semaphore);
NSLog(@"2 %@",self.array);
[self.array removeAllObjects];
}
-(void)test2 {
[self.array addObject:@"test 2"];
[self.array addObject:@"test 3"];
dispatch_semaphore_wait(self.semaphore, DISPATCH_TIME_FOREVER);
[self.array addObject:@"test 5"];
dispatch_semaphore_signal(self.semaphore);
NSLog(@"3 %@",self.array);
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment