How to cancel NSBlockOperation

Doh. Dear future googlers: of course operation is nil when copied by the block, but it doesn’t have to be copied. It can be qualified with __block like so: //THIS MIGHT LEAK! See the update below. __block NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{ while( ! [operation isCancelled]){ //do something… } }]; UPDATE: Upon further meditation, it … Read more

dispatch_sync vs. dispatch_async on main queue

This is a common issue related to disk I/O and GCD. Basically, GCD is probably spawning one thread for each file, and at a certain point you’ve got too many threads for the system to service in a reasonable amount of time. Every time you call dispatch_async() and in that block you attempt to to … Read more

Why can’t we use a dispatch_sync on the current queue?

dispatch_sync does two things: queue a block blocks the current thread until the block has finished running Given that the main thread is a serial queue (which means it uses only one thread), if you run the following statement on the main queue: dispatch_sync(dispatch_get_main_queue(), ^(){/*…*/}); the following events will happen: dispatch_sync queues the block in … Read more

Using weak self in dispatch_async function

Assuming, self is an object pointer to a UIViewController. Things to consider: A UIViewController is a “UIKit” object. UIKit objects shall not be sent methods on non-main threads, that is – those methods must execute on the main thread only! A block that has been enqueued in a queue – whether this was synchronously or … Read more

How to dispatch on main queue synchronously without a deadlock?

I need to use something like this fairly regularly within my Mac and iOS applications, so I use the following helper function (originally described in this answer): void runOnMainQueueWithoutDeadlocking(void (^block)(void)) { if ([NSThread isMainThread]) { block(); } else { dispatch_sync(dispatch_get_main_queue(), block); } } which you call via runOnMainQueueWithoutDeadlocking(^{ //Do stuff }); This is pretty much … Read more

Which is the right one, nil or NULL, to mark “no Objective-C block”?

Blocks are not represented as function pointers. They’re represented as blocks, and this is denoted by the ^ symbol in their declaration. Down under the hood, the only resemblance is the call syntax. Otherwise, they are both very, very different. It is often useful to call methods on them. For instance, if you don’t use … Read more

UIButton block equivalent to addTarget:action:forControlEvents: method?

I just implemented this. It work’s like a charm! And it wasn’t even hard. typedef void (^ActionBlock)(); @interface UIBlockButton : UIButton { ActionBlock _actionBlock; } -(void) handleControlEvent:(UIControlEvents)event withBlock:(ActionBlock) action; @end @implementation UIBlockButton -(void) handleControlEvent:(UIControlEvents)event withBlock:(ActionBlock) action { _actionBlock = action; [self addTarget:self action:@selector(callActionBlock:) forControlEvents:event]; } -(void) callActionBlock:(id)sender{ _actionBlock(); } @end

Block implicitly retains ‘self’; explicitly mention ‘self’ to indicate this is intended behavior

Replacing myTimer by self->myTimer would fix your warning. When you use an iVar _iVar in the code, the compiler will replace the code by self->_iVar, and if you use it inside a block, the block will capture self instead of the iVar itself. The warning is just to make sure that the developer understand this … Read more

Implementing a method taking a block to use as callback

You can call a block like a regular function: BOOL finished = …; if (completion) { completion(finished); } So that means implementing a complete block function using your example would look like this: + (void)myMethod:(UIView *)exampleView completion:(void (^)(BOOL finished))completion { if (completion) { completion(finished); } }

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)