UIManagedDocument:传递给完成处理程序的块永远不会被调用

时间:2014-03-26 14:07:24

标签: ios core-data uimanageddocument

嗨,这是我为在JIPManagedDocument.m中处理我的UIManagedDocument而创建的代码:

+(JIPManagedDocument *)sharedManagedDocument
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
    NSURL *url = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
    url = [url URLByAppendingPathComponent:@"DemoDocument"];
    _sharedManagedDocument = [[JIPManagedDocument alloc] initWithFileURL:url];
    });

   return _sharedManagedDocument;
}

-(void)performBlockWithDocument:(void (^)(JIPManagedDocument * managedDocument))block
{
    void (^completionBlock) (BOOL) = ^(BOOL success) 
    {
        if (success)
        {
          block(self);
          NSLog(@"COULD PERFORM BLOCK WITH ManagedDocument");
        }
        else
        {
          NSLog(@"COULDNT PERFORM BLOCK WITH ManagedDocument");
        }
        self.openingDocument = NO;
    };

    if (self.documentState == UIDocumentStateNormal)
    {
        completionBlock(YES);
    }

    else if (! self.openingDocument)
    {
       self.openingDocument = YES;

       if ( ! [[NSFileManager defaultManager] fileExistsAtPath:[self.fileURL path]] )
       {
           [self        saveToURL:self.fileURL
                 forSaveOperation:UIDocumentSaveForCreating
               completionHandler:completionBlock];
        }


        else if (self.documentState == UIDocumentStateClosed)
        {
           [self openWithCompletionHandler:completionBlock];
        }
    }  
}

然后当我尝试调用这样的方法时:

[[JIPManagedDocument sharedManagedDocument] performBlockWithDocument:^(JIPManagedDocument *managedDocument)
 {
     //Do something
 }];

//Do something部分永远不会被执行。

非常感谢任何帮助,非常感谢!

1 个答案:

答案 0 :(得分:0)

作为参数传递的块,即调用//做某事的块。你只能在那里打电话:

if (success)
{
     block(self);
     NSLog(@"COULD PERFORM BLOCK WITH ManagedDocument");
}

你确定,这个条件已经实现了吗?