我如何使用块创建一个全局UIManagedDocument实例,该实例每个文档共享一个磁盘,并由我的整个应用程序使用?

11

我试图设计一个辅助方法以检索UIManagedDocument,然后打开并返回它,以便我可以从我的应用程序的多个位置访问同一个UIManagedDocument。

由于我对块不太熟悉,所以我遇到了异步性方面的困难。理想情况下,事件序列如下:

  1. 类X调用帮助方法来检索UIManagedDocument,并包括一段代码块在返回打开的文档时运行。
  2. 类方法检索UIManagedDocument并根据需要调用openWithCompletionHandler或saveToURL,并包括代码块以在返回打开的文档时运行。
  3. openwithCompletionHandler或saveToURL完成其任务,并以成功= YES返回并运行其代码块
  4. 类方法完成其任务并返回已打开的UIManagedDocument,并在其代码块中运行。

我能否通过某种方式传递原始块?

这是我的代码。非常感谢您的任何想法。

// This is a dictionary where the keys are "Vacations" and the objects are URLs to UIManagedDocuments
static NSMutableDictionary *managedDocumentDictionary = nil;

// This typedef has been defined in .h file: 
// typedef void (^completion_block_t)(UIManagedDocument *vacation);
// The idea is that this class method will run the block when its UIManagedObject has opened

@implementation MyVacationsHelper

+ (void)openVacation:(NSString *)vacationName usingBlock:(completion_block_t)completionBlock
{
    // Try to retrieve the relevant UIManagedDocument from managedDocumentDictionary
    UIManagedDocument *doc = [managedDocumentDictionary objectForKey:vacationName];

    // Get URL for this vacation -> "<Documents Directory>/<vacationName>" 
    NSURL *url = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
    url = [url URLByAppendingPathComponent:vacationName];

    // If UIManagedObject was not retrieved, create it
    if (!doc) {

        // Create UIManagedDocument with this URL
        doc = [[UIManagedDocument alloc] initWithFileURL:url];

        // Add to managedDocumentDictionary
        [managedDocumentDictionary setObject:doc forKey:vacationName];
    }

    // If document exists on disk...

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

    } else {

        [doc saveToURL:url 
      forSaveOperation:UIDocumentSaveForCreating
     completionHandler:^(BOOL success)
        { 
            // As per comments above
        }];

    }

}
2个回答

7
您可以使用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);
     }];

假设您已经在调用openVacation方法的类中实现了以下方法:
-(void)vacationOpened:(UIManagedDocument *)vacation
{
    NSLog(@"My Vacation: %@", vacation.description);
}

调用openVacation方法的示例代码如下所示:
[MyVacationsHelper openVacation:@"MyVacation1" usingBlock:^(UIManagedDocument *vacation){
    [self vacationOpened:vacation];
}];

在这个代码片段中,符号后的(UIManagedDocument *vacation)意味着当您使用括号表示法执行块时--例如completionBlock(doc)--,您需要将(UIManagedDocument *)列为参数。这个参数的值将在指定的块内被称为vacation。在上面的块代码示例中,我调用了我的当前类(self)中的一个方法,并将该参数传递给该方法,以便在需要时使用它(我只是在这里做了一个NSLog来确保它工作正常)。

3

作者不再建议使用uimanageddocument来完成此操作。 - SmileBot

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接