【问题标题】:iOS CoreDataTableViewController table when UIManagedDocument is created using saveToURL使用 saveToURL 创建 UIManagedDocument 时的 iOS CoreDataTableViewController 表
【发布时间】:2013-04-23 07:56:55
【问题描述】:

我正在使用斯坦福大学的 CoreDataTableViewController 来显示动态表格。

应用程序如何工作:要向此表添加一行,会弹出一个子屏幕用于数据输入,当子屏幕关闭时,插入新创建的 managedObject,并通过回调重新加载表。

我注意到在重新进入屏幕时,新添加的行会间歇性地从表中丢失。我注意到这种行为仅在第一次使用 saveToURL 创建 UIManagedDocument 对象时发生。随后,在重新启动应用程序并使用 openWithCompletionHandler 打开 UIManagedDocument 后,列表始终正确显示。

这是我用来创建/打开 UIManagedDocument 的代码:

- (void)performWithDocument:(OnDocumentReady)onDocumentReady
{
    void (^OnDocumentDidLoad)(BOOL) = ^(BOOL success)
    {
        NSLog(@"Called OnDocumentDidLoad");
        onDocumentReady(self.document);
    };

    if (![[NSFileManager defaultManager] fileExistsAtPath:[self.document.fileURL path]])
    {
        [self.document saveToURL:self.document.fileURL
                forSaveOperation:UIDocumentSaveForCreating
               completionHandler:OnDocumentDidLoad];
    }
    else if (self.document.documentState == UIDocumentStateClosed)
    {
        NSLog(@"Calling openWithCompletionHandler:OnDocumentDidLoad");
        [self.document openWithCompletionHandler:OnDocumentDidLoad];
    }
    else if (self.document.documentState == UIDocumentStateNormal)
    {
        OnDocumentDidLoad(YES);
    }
}

运行上述代码时,始终调用onDocumentDidLoad(),成功标志为YES。

任何帮助将不胜感激。提前致谢!

-- 编辑以下代码以显示我的创建方法,然后关闭并重新打开文档以响应 Jody - 我仍然遇到同样的问题。 ---

- (void)performWithDocument:(OnDocumentReady)onDocumentReady
{
    void (^OnDocumentDidLoad)(BOOL) = ^(BOOL success)
    {
        NSLog(@"Called OnDocumentDidLoad success: %d", success);
        onDocumentReady(self.document);
    };

    void (^OnDocumentDidClose)(BOOL) = ^(BOOL success)
    {
        NSLog(@"Called OnDocumentDidClose, openWithCompletionHandler success: %d", success);
        [self.document openWithCompletionHandler:OnDocumentDidLoad];
    };

    void (^OnDocumentDidSave)(BOOL) = ^(BOOL success)
    {
        NSLog(@"Called OnDocumentDidSave success: %d", success);
        if (self.document.documentState == UIDocumentStateClosed)
        {
            NSLog(@"Calling openWithCompletionHandler:OnDocumentDidLoad from onDocumentDidSave - was closed");
            [self.document openWithCompletionHandler:OnDocumentDidLoad];
        }
        else if (self.document.documentState == UIDocumentStateNormal)
        {
            // TODO Close and reopen?
            NSLog(@"Calling closeWithCompletionHandler:OnDocumentDidClose from onDocumentDidSave - was normal");

            [self.document closeWithCompletionHandler:OnDocumentDidClose];
        }
    };

    if (![[NSFileManager defaultManager] fileExistsAtPath:[self.document.fileURL path]])
    {
        // Database doesn't exist, create it, then close and reopen in completion handler
        [self.document saveToURL:self.document.fileURL
                forSaveOperation:UIDocumentSaveForCreating
               completionHandler:OnDocumentDidSave];
    }
    else if (self.document.documentState == UIDocumentStateClosed)
    {
        // Open existing database
        NSLog(@"Calling openWithCompletionHandler:OnDocumentDidLoad");
        [self.document openWithCompletionHandler:OnDocumentDidLoad];
    }
    else if (self.document.documentState == UIDocumentStateNormal)
    {
        // Already opened
        OnDocumentDidLoad(YES);
    }
}

【问题讨论】:

    标签: core-data uimanageddocument


    【解决方案1】:

    您没有指明您使用的是哪个版本的 iOS,这几乎总是很重要的。

    创建文档时出现 iOS 5 问题。我很久以前在创建UIManagedDocument 时采用了以下方法。它仍然在我的库代码中,因为我从来没有足够的动力花时间查看问题是否得到解决。

    如果文档不存在,请将其保存以供创建,并在完成处理程序中将其关闭,然后在该完成处理程序中将其打开。这样一来,除非它已经存在,否则我从未真正使用过文档。

    【讨论】:

    • 感谢您的回复。我在 iOS 6 上。是的,我也尝试在完成处理程序中关闭它并重新打开它,但它没有任何区别 - 空白行仍然出现。
    • 我已经添加了代码,其中包括我重新打开新创建的文档的努力。它看起来正确吗?不幸的是,空白行仍然间歇性地出现。和以前一样,杀死应用程序后重新启动它可以解决问题,但显然这是不可接受的。
    • IMO,通过将块定义为变量,您正在失去参考优势的局部性。无论如何,它看起来有些合理。您确定在尝试填充文档之前等待异步回调到达吗?就个人而言,在文档实际打开并准备就绪之前,我不会设置文档属性……这可以防止过早使用。
    • 嗨乔迪再次感谢您的回复。据我所知,我看不出代码中的逻辑有缺陷——一切都在 onDocumentReady 中完成。您是否有可能发布代码的 sn-ps 来说明您是如何做到的?我对此一无所知。再次感谢。
    • 我认为您的问题出在其他地方,与文档的打开方式无关。
    猜你喜欢
    • 1970-01-01
    • 2012-08-17
    • 2012-08-22
    • 1970-01-01
    • 1970-01-01
    • 2013-04-15
    • 1970-01-01
    • 2013-03-04
    • 2012-05-25
    相关资源
    最近更新 更多