How do I create a global UIManagedDocument instance per document-on-disk shared by my whole application using blocks?

前端 未结 2 1497
执笔经年
执笔经年 2020-12-09 05:26

I am trying to design a helper method which will retrieve a UIManagedDocument, then open and return it, so that I may access the same UIManagedDocument from several places i

相关标签:
2条回答
  • 2020-12-09 06:02

    You can execute the block with completionBlock(doc).

        [doc openWithCompletionHandler:^(BOOL success) 
         {
             // Can I call the completionBlock from above in here?
             // How do I pass back the opened UIDocument
            completionBlock(doc);
         }];
    

    Let's assume that you have the following method implemented in the class that will be calling your openVacation method:

    -(void)vacationOpened:(UIManagedDocument *)vacation
    {
        NSLog(@"My Vacation: %@", vacation.description);
    }
    

    A sample line of code that would call your openVacation method would be:

    [MyVacationsHelper openVacation:@"MyVacation1" usingBlock:^(UIManagedDocument *vacation){
        [self vacationOpened:vacation];
    }];
    

    The (UIManagedDocument *vacation) after the caret means that when you execute the block using the parentheses notation -- as in completionBlock(doc) --, you need to list a (UIManagedDocument *) as a parameter. The value of that parameter will be referred to as vacation inside the specified block. What I did in my block code sample above was call a method in my current class (self) and pass the parameter along to that method so that I could use it as needed (I just did an NSLog here to make sure that it worked).

    0 讨论(0)
  • 2020-12-09 06:15

    I found a pretty helpful article - "Core Data with a Single Shared UIManagedDocument"

    0 讨论(0)
提交回复
热议问题