Post

Replies

Boosts

Views

Activity

Variable not captured unless referencing explicitly from local assignment
I'm trying to modify a shared __block BOOL variable for the purposes of flagging of execution to proceed. I implemented it successfully by creating an object that encapsulates the BOOL variable, but when sending the block inline, there are issues--I must create a local variable for the block and explicitly reference the BOOL variable inside of it (marked 1A in code sample). Any ideas how to make the - (void)primitive_cancellable_dispatch_after:(dispatch_time_t)delay block:(MyBlock)block stop:(BOOL*)stop method work without this requirement from the call site? It seems that the variable is not being captured unless it is being assigned locally and referenced. In general, at what point is the variable captured if not being explicitly referenced locally? dispatch_time_t delay = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)); __block BOOL stop; // =============== 1A =============== MyBlock primitiveBlock = ^{ NSLog(@"Ran primitive implementation via assignment. (stop: %@ / %p)", @(stop), &stop); // stop only "captured" when referencing within a locally assigned block, if this line is commented out, stop isn't updated }; // =============== 1A =============== [self primitive_cancellable_dispatch_after:delay block:^{ NSLog(@"Ran primitive implementation via inline. (stop: %@ / %p)", @(stop), &stop); } stop:&stop]; __block Task *task = [self reference_cancellable_dispatch_after:delay block:^{ NSLog(@"Ran reference implementation: (task.shouldCancel: %@)",@(task.shouldCancel)); }]; task.shouldCancel = NO; // works as expected stop = NO; // stop isn't updated as expected, when checking value within "primitive_cancellable_dispatch_after", always returns no } - (void)primitive_cancellable_dispatch_after:(dispatch_time_t)delay block:(MyBlock)block stop:(BOOL*)stop { dispatch_after(delay, dispatch_get_main_queue(), ^{ BOOL shouldStop = *stop; // stop doesn't seem to resolving correctly, stop is always "NO", unless assigning a local block outside NSLog(@"Checking primitive stop (shouldStop: %@ / %p)", @(shouldStop), stop); if(!shouldStop && block) { block(); NSLog(@"Checking primitive stop (shouldStop: %@ / %p)", @(shouldStop), stop); } }); } /*@interface Task : NSObject @property (nonatomic, assign) BOOL shouldCancel; @property (nonatomic, strong) Task *task; @end @implementation Task @end*/ - (Task*)reference_cancellable_dispatch_after:(dispatch_time_t)delay block:(MyBlock)block { Task *task = [Task new]; dispatch_after(delay, dispatch_get_main_queue(), ^{ if(!task.shouldCancel && block) { block(); } }); return task; }
9
0
1.9k
Jun ’21
What are the benefits to Apple's Dispatch Queue API designing for future functionality?
What are the benefits of the design of the queue generation / retrieval APIs as opposed to having helper functions that simply pass along the default value at the time they introduced the API? Examples: dispatch_queue_global_t dispatch_get_global_queue(intptr_t identifier, uintptr_t flags) dispatch_queue_t dispatch_queue_create(const char *label, dispatch_queue_attr_t attr) The necessitation to send magic values such as NULL and 0 as parameters seems a bit verbose and was curious if there are any benefits or the motivation for designing them in this way since it seems that introducing the new methods would be a non-breaking change.
5
0
893
Jun ’21
Having trouble refactoring
I'm having trouble refactoring a class. I've tried everything under the sun that has been suggested on various other forums (clear derived data and re-build as well as re-copying project folder) but I can't seem to get the error message to go away and refactor successfully. I have tried highlighting the class name in the .h/.m files, then refactoring, and it does not work like it previously had. Any ideas how to resolve the issue?I've tried on Xcode 7.1 (7B91b) as well as 7.2 beta (7C46l). My project is 100% Obj-c.Error:The selection is not a type that can be renamed.Make a different selection and try again.
3
0
4.2k
Nov ’15