Facebook Interview Question for iOS Developers


Country: United States
Interview Type: Phone Interview




Comment hidden because of low score. Click to expand.
0
of 0 vote

There's no technique to cancel blocks which were defined within a dispatch_after call. There's no handle or reference to them.

- Felipe February 19, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Yes. There's no technique to cancel blocks which were defined within a dispatch_after call.

But you can use a boolean that wrap the block to determine whether execute in the block or not.

- read010931 May 02, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Inside of the dispatch_after function block parameter, add a flag to determine if the code is to be run or not. If the flag is set to cancel when the block is executed, nothing will happen and effectively cancels the block.

- ekbiker May 05, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

// The dispatch_cancel_block_t takes as parameter the "cancel" directive to suspend the block execution or not whenever the block to execute is dispatched.
// The return value is a boolean indicating if the block has already been executed or not.
typedef BOOL (^dispatch_cancel_block_t)(BOOL cancelBlock);

dispatch_cancel_block_t dispatch_async_with_cancel_block(dispatch_queue_t queue, void (^block)())
{
__block BOOL execute = YES;
__block BOOL executed = NO;

dispatch_cancel_block_t cancelBlock = ^BOOL (BOOL cancelled) {
execute = !cancelled;
return executed == NO;
};

dispatch_async(queue, ^{
if (execute)
block();
executed = YES;
});

return cancelBlock;
}

- (void)testCancelableBlock
{
dispatch_cancel_block_t cancelBlock = dispatch_async_with_cancel_block(dispatch_get_main_queue(), ^{
NSLog(@"Block 1 executed");
});

// Canceling the block execution
BOOL success1 = cancelBlock(YES);
NSLog(@"Block is cancelled successfully: %@", success1?@"YES":@"NO");

// Resuming the block execution
// BOOL success2 = cancelBlock(NO);
// NSLog(@"Block is resumed successfully: %@", success2?@"YES":@"NO");
}

- Joan Martin November 06, 2014 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More