objective-c-blocks

Clarification on Apple's Block Docs?

試著忘記壹切 提交于 2019-12-01 01:25:19
I am working through some retain-cycle issues with blocks/ARC, and I am trying to get my head around the nuances. Any guidance is appreciated. Apple's documentation on "Blocks and Variables" (http://developer.apple.com/library/ios/#documentation/cocoa/Conceptual/Blocks/Articles/bxVariables.html) says the following: If you use a block within the implementation of a method, the rules for memory management of object instance variables are more subtle: If you access an instance variable by reference, self is retained; If you access an instance variable by value, the variable is retained. The

Why is Clang confused by @try{} in a block with no return statement?

拈花ヽ惹草 提交于 2019-12-01 00:37:53
问题 Under normal conditions, when a block is declared to return a value, but no return statement actually appears in the block, Clang fails to compile it with an error (of a missing return value). However, this breaks when that block contains @try{} @catch(...){} or @try{} @finally{} . Does anyone know why? The way I found this was when using @weakify() and @strongify() in RACExtScope in ReactiveCocoa, in one block I forgot to return a signal. But the compiler didn't warn me and crashed on

How to call an objective-c function which accepts Dictionary of blocks as argument from Swift?

旧街凉风 提交于 2019-12-01 00:32:26
I have a function in my objective c file (lets say class MyBlockExecutor): + (void) runBlockFromDictionary: (NSDictionary*) blocksDict andKey: (NSString*) key { if ( [blocksDict objectForKey: key] != nil ) { ((MyBlock)[blocksDict objectForKey: key])(); } } Now, I want to call this function from Swift. Here is my swift call: MyBlockExecutor.runBlock(from: [ "key1":{ ()->Void in print("block for key1 called") } ], andKey: "key1") This crashes my app. I am getting EXC_BAD_ACCESS error on this line: ((MyBlock)[blocksDict objectForKey: key])(); Although, calling the same function from Objective-C

NSTimer Category + Blocks implementation to replace selector

江枫思渺然 提交于 2019-12-01 00:05:28
I am quite new to blocks and objective-c, and i am trying to write my first category using both. My idea is to create a category on NSTimer that will receive a block as a parameter and this block will be used in the selector call. Right now I have this. // NSTimer+Additions.h #import <Foundation/Foundation.h> typedef void (^VoidBlock)(); @interface NSTimer (NSTimer_Additions) + (NSTimer *)scheduleTimerWithTimeInterval:(NSTimeInterval)theSeconds repeats:(BOOL)repeats actions:(VoidBlock)actions; @end #import "NSTimer+Additions.h" static VoidBlock _voidBlock; @interface NSTimer (AdditionsPrivate)

ios store ^block in dictionary or array?

佐手、 提交于 2019-11-30 23:14:57
问题 Can I store ^block in a dictionary or an array? I need to listen to a server notification which I need to provide a block to handle the notification, and in my project several view controllers all want to hear the notification, so I made a generic notification manager, which has its own block for handling server notification and it has an array of delegates, so in the manager's block: - (^)(NSString *message){ for (delegate in allDelegates) { delegate.handlerBlock(message); } } but can I

Keep object alive until a background task finishes

陌路散爱 提交于 2019-11-30 22:34:39
I am trying to implement a method that executes a task in the background and then calls a block on the main thread: + (void)migrateStoreWithCompletionHandler:(MigrationControllerCompletion)completion { MigrationController *controller = [[MigrationController alloc] initWithCompletionBlock:completion]; [controller migrateStore]; } This is the -initWithCompletionBlock: method: - (id)initWithCompletionBlock:(MigrationControllerCompletion)completion { self = [super init]; if (self) { _completion = [completion copy]; } return self; } The background work happens in -migrateStore . The problem is that

does dispatch_async copy internal blocks

与世无争的帅哥 提交于 2019-11-30 21:37:03
Given the following (manual reference counting): void (^block)(void) = ^ { NSLog(@"wuttup"); } void (^async_block)(void) = ^ { block(); } dispatch_async(dispatch_get_main_queue(), async_block); Will "block" be copied rather than thrown off the stack and destroyed? I believe, the answer is Yes. The outer block will be asynchronously dispatched which causes the runtime to make a copy on the heap for this block. And as shown below, and described in the Block Implementation Specification - Clang 3.4 Documentation , the inner block's imported variables are also copied to the heap. In the OP's

How can a weakly retained block cause a retain cycle when capturing “self”

天涯浪子 提交于 2019-11-30 20:24:06
问题 I have a class with a property which is a weak reference to a block. @interface BlockTest : NSObject @property (nonatomic, weak) void(^testBlock)(); @end At another point in the class I use this block like this: - (void)foobar { self.testBlock = ^{ [self doSomething]; }; } The compiler (Apple LLVM 3.0) complains that there might be a retain cycle because self is strongly captured here. But I fail to see how this leads to a retain cycle because the block itself is a __weak reference, so it

Capturing a variable in a Block when the Block is in the initializer

半腔热情 提交于 2019-11-30 20:22:18
Consider this: id observer = [[NSNotificationCenter defaultCenter] addObserverForName:MyNotification object:nil queue:nil usingBlock:^(NSNotification *note) { [[NSNotificationCenter defaultCenter] removeObserver:observer name:MyNotification object:nil ]; // do other stuff here... } ]; I'm using this pattern to observe a notification once and then stop observing it. But LLVM tells me (under ARC) that Variable 'observer' is uninitialized when captured by block. How can I fix this, since the block necessarily captures the variable before initialization, it being part of the initializer? Will

Why is a __block variable is moved to the heap BEFORE the block is copied? [closed]

一曲冷凌霜 提交于 2019-11-30 19:05:26
I know that a __block variable will be moved to the heap from the stack if a Block accessing it was copied. But the following test code show me that the __block variable is moved to the heap before the Block's copying. That is, the four outputs are: stack => heap => heap => heap, which is not my expected result: stack => stack => stack => heap. Could someone straighten me out? __block int x = 0; int *pointerToX = &x; //1. It's on the stack NSLog(@"x's location is on the stack: %p", &x); int (^block)() = ^{ x += 1; return x; }; //2. I think its stack, but it's heap NSLog(@"x's location is on