Domanda

While working with Core Data, I have found that my UIManagedDocument object has a documentState equal to 5. The UIDocument documentation only defines these constants:

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

Which would be 0, 1, 2, 4 and 8. 5 might be a special state that UIManagedDocument uses, but I can't find it documented anywhere. The state seems to occur when the Core Data schema is changed. I don't know what the state means. I usually get the error: Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'This NSPersistentStoreCoordinator has no persistent stores. It cannot perform a save operation.', which makes sense because the document needs to be opened in a normal state in order to be used as the persistent store.

Right now I am just checking for the state being equal to 5 and deleting the persistent store and recreating it when this occurs. But once my app is live and storing user data, I won't want to be doing this. I haven't looked into best practices for migrating Core Data schemas, but it seems kind of messy too to be checking for managedDocument.documentState == 5 in my code. Is there not any documentation on this document state anywhere?

Update: Now that I'm looking at it, it would make sense that the reason these constants are defined the way they are is so that they can be bitwise ored together as masks. So a documentState equal to 5 would imply it is both UIDocumentStateClosed as well as UIDocumentStateSavingError. These errors are pretty general though. How would I go about narrowing the root cause?

Also, all the example code I've seen for checking for these document states show checking for equality, i.e. if (managedDocument.documentState == UIDocumentStateClosed), but this would imply this is not correct and should rather be checked with a bitwise and, i.e. if (managedDocument.documentState & UIDocumentStateClosed).

È stato utile?

Soluzione

I do not have much experience with UIDocument, but it seems to me that the document state is a bit mask that is the combination of several states, so that documentState == 5 == 1 + 4 means UIDocumentStateClosed + UIDocumentStateSavingError.

In the Document-Based App Programming Guide for iOS you see that documentState is never checked with ==, but always tested against bit masks, for example:

-(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];
    }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top