简体   繁体   中英

UIManagedDocument : block passed to completion handler never gets called

Hi, here's the code I created to handle my UIManagedDocument in JIPManagedDocument.m :

+(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];
        }
    }  
}

And then when I try to call the method like this :

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

The //Do something part never gets executed.

Any help would be very appreciated, thanks a lot !

The block you pass as parameter, that is the one that will call the //Do something. You only call it there :

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

Are you sure that, this condition is fulfilled ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM