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

Posted

技术标签:

【中文标题】如何使用块为我的整个应用程序共享的每个磁盘上的文档创建一个全局 UIManagedDocument 实例?【英文标题】:How do I create a global UIManagedDocument instance per document-on-disk shared by my whole application using blocks? 【发布时间】:2012-02-09 00:56:55 【问题描述】:

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

由于我对块不太熟悉,因此我对它的异步性质感到困惑。理想情况下,事件的顺序是这样的:

    X 类调用辅助方法来检索 UIManagedDocument,并包含一个代码块,以便在返回打开的文档时运行。 类方法检索 UIManagedDocument 并根据需要调用 openWithCompletionHandler 或 saveToURL,并包含一个代码块,以便在返回打开的文档时运行。 openwithCompletionHandler 或 saveToURL 完成它们的任务,并返回 success = YES 并运行其块中的代码 类方法完成其任务并返回一个打开的 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
        ];

    


【问题讨论】:

【参考方案1】:

您可以使用 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 以确保它有效) .

【讨论】:

【参考方案2】:

我发现了一篇很有帮助的文章——“Core Data with a Single Shared UIManagedDocument”

【讨论】:

作者不再推荐使用 uimanageddocument 进行此操作。

以上是关于如何使用块为我的整个应用程序共享的每个磁盘上的文档创建一个全局 UIManagedDocument 实例?的主要内容,如果未能解决你的问题,请参考以下文章

如何在整个应用程序中共享CLLocation?

怎样将mac上的文件共享,在另一台windows电脑上使用?

如何共享 Amazon EB 实例

如何使用 Android Studio 查看共享首选项文件?

如何快速找到添加/删除的文件?

微服务架构中如何在微服务之间共享java模型