【问题标题】:UIManagedDocument documentState == 5, undefined state constantUIManagedDocument documentState == 5,未定义状态常量
【发布时间】:2013-04-20 05:19:34
【问题描述】:

在使用 Core Data 时,我发现我的 UIManagedDocument 对象的 documentState 等于 5。UIDocument documentation 只定义了这些常量:

enum {     UIDocumentStateNormal          = 0,
   UIDocumentStateClosed          = 1 << 0,
   UIDocumentStateInConflict      = 1 << 1,
   UIDocumentStateSavingError     = 1 << 2,
   UIDocumentStateEditingDisabled = 1 << 3   }; typedef NSInteger UIDocumentState;

这将是 0、1、2、4 和 8。5 可能是 UIManagedDocument 使用的特殊状态,但我在任何地方都找不到它的文档。当核心数据模式改变时,状态似乎发生了。我不知道国家是什么意思。我通常会收到错误:Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'This NSPersistentStoreCoordinator has no persistent stores. It cannot perform a save operation.',这是有道理的,因为文档需要以正常状态打开才能用作持久存储。

现在我只是检查状态是否等于 5,并在发生这种情况时删除持久存储并重新创建它。但是一旦我的应用程序上线并存储用户数据,我就不想这样做了。我还没有研究过迁移核心数据模式的最佳实践,但是在我的代码中检查managedDocument.documentState == 5 似乎也有点混乱。任何地方都没有关于此文档状态的任何文档吗?

更新: 现在我正在查看它,这些常量按原样定义的原因是可以将它们按位或一起作为掩码进行按位或运算,这是有道理的。所以 documentState 等于 5 意味着它既是 UIDocumentStateClosed 也是 UIDocumentStateSavingError。不过,这些错误非常普遍。我将如何缩小根本原因?

此外,我看到的用于检查这些文档状态的所有示例代码都显示检查是否相等,即if (managedDocument.documentState == UIDocumentStateClosed),但这意味着这是不正确的,应该按位检查,即@987654333 @。

【问题讨论】:

    标签: core-data uimanageddocument schema-migration


    【解决方案1】:

    我对 UIDocument 没有太多经验,但在我看来文档状态 是一个位掩码,它是几个状态的组合,所以documentState == 5 == 1 + 4 表示UIDocumentStateClosed + UIDocumentStateSavingError

    Document-Based App Programming Guide for iOS 中,您会看到documentState 从未与== 一起检查,但始终针对位掩码进行测试, 例如:

    -(void)documentStateChanged {
        UIDocumentState state = _document.documentState;
        [_statusView setDocumentState:state];
        if (state & UIDocumentStateEditingDisabled) {
            [_textView resignFirstResponder];
        }
        if (state & UIDocumentStateInConflict) {
            [self showConflictButton];
        }
        else {
            [self hideConflictButton];
            [self dismissModalViewControllerAnimated:YES];
        }
    }
    

    【讨论】:

    • 我在您回答时得出了这个结论。您对如何查找保存错误状态的来源有任何帮助吗?就像我说的,我假设这是因为架构更改,但我如何确定这一点?
    • @JeffLockhart:我只尝试回答您问题的第一个版本。很遗憾,我无法为您提供第二个版本。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-19
    • 1970-01-01
    • 2018-05-16
    • 2020-11-15
    • 2018-04-21
    • 1970-01-01
    相关资源
    最近更新 更多