Objective c 完成块未执行

Objective c 完成块未执行,objective-c,ios,core-data,block,Objective C,Ios,Core Data,Block,下面是对该块的调用: [VacationHelper openVacationWithName:vacationName usingBlock:^(UIManagedDocument *vacationDocument) { NSLog(@"vacationDocument.description:%@", vacationDocument.description); }]; 在接收方法的.h中: typedef void (^completion_blo

下面是对该块的调用:

[VacationHelper openVacationWithName:vacationName
                usingBlock:^(UIManagedDocument *vacationDocument) {
   NSLog(@"vacationDocument.description:%@", vacationDocument.description);
}];
在接收方法的.h中:

typedef void (^completion_block_t)(UIManagedDocument *vacationDocument);
在.m中:

+ (void)openVacationWithName:(NSString *)vacationName
                  usingBlock:(completion_block_t)completionBlock;
{
    NSLog(@"Opening Vacation Document");

    // Get documents directory and path.
    NSURL *url = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
    url        = [url URLByAppendingPathComponent:vacationName];

    // Create the document and open if a match exists on file.
    UIManagedDocument *vacationDocument = [[UIManagedDocument alloc] initWithFileURL:url];
    if ([[NSFileManager defaultManager] fileExistsAtPath:[url path]]) {
        NSLog(@"vacationDocument.documentState:%i", vacationDocument.documentState);
        [vacationDocument openWithCompletionHandler:^(BOOL success) {
            if (success) NSLog(@"Document was opened.");
            else NSLog (@"Couldn't open document at %@", url);
        }];
    } else {

        // No match exists, so save the document to file.
        [vacationDocument saveToURL:url forSaveOperation:UIDocumentSaveForCreating
                  completionHandler:^(BOOL success) {
                      if (success) NSLog(@"Document was created.");
                      else NSLog(@"Couldn't create document at %@", url);
                  }];
    }
    NSLog(@"Exiting helper.");
}
我的问题是,为什么执行没有到达调用
openVacationWithName:
时传递的块?我从来没有看到过写NSLog


我怀疑
openVacationWithName:
没有完成,但是“Exiting helper.”的NSLog会打印出来。感谢您的指导。仅供参考,这是为iTunes U/Stanford CS193P作业#6。

您没有在
OpenVacation内调用
completionBlock
,其名称为:usingBlock:
。也许您希望在方法的末尾这样做:

  ...
  if (completionBlock) {
    completionBlock(vacationDocument);
  }

  NSLog(@"Exiting helper.");
}
(在这种情况下,只返回
UIManagedDocument
,可能更有意义)

或者可能在方法
openWithCompletionHandler:
saveToUrl:forSaveOperation:completionHandler:
中的
completionHandler
s中:

... ^(BOOL success) {
  if (success) {
    NSLog(@"Document was created.");
  }
  else {
    NSLog(@"Couldn't create document at %@", url);
  }

  if (completionBlock) {
    completionBlock(vacationDocument);
  }
} ...

谢谢你,Jordão。我假设一旦调用的方法完成,传递的块将自动执行。如上所述,插入通话有效。。。完成块(休假文档);NSLog(@“正在退出帮助程序”);我想在这里提到良好实践,wrap块在if语句中调用:
if(block)block(),因此可以使用nil完成处理程序调用您的函数,没有例外。谢谢@kelin,您完全正确,我将相应地更改代码。