UIManagedDocument - Validating Core Data Entity

491 Views Asked by At

I have an app that uses Core Data and it gets its ManagedObjectContext by using UIManagedObject. From reading, I see that I am not suppose to save the context directly - rather I should depend on autosaving of UIManagedObject or use saveToURL:... My issue is that I want to validate the data being stored in my entity. I have constraints on the entity that specify that the min length for the string properties is 1. However, I can create a new object, assign its properties empty strings, and save the file. In the completion handler of saveToURL:... it always has a true success value. I then created my own validator for the name property of my entity. I used sample code from the Core Data Programming Guide -

-(BOOL)validateName:(id *)ioValue error:(__autoreleasing NSError **)outError 

{
    if (*ioValue == nil) 

    {
        if (outError != NULL) 

        {
            NSString *errorStr = @"nil error";

            NSDictionary *userInfoDict = [NSDictionary dictionaryWithObject:errorStr

                forKey:NSLocalizedDescriptionKey];

            NSError __autoreleasing *error = [[NSError alloc] initWithDomain:@"domain"

                code:1

                userInfo:userInfoDict];

            *outError = error;

        }

        return NO;

    }

    else if( [*ioValue length] == 0 )

    {

        if (outError != NULL) {

            NSString *errorStr = @"length error";

            NSDictionary *userInfoDict = [NSDictionary dictionaryWithObject:errorStr

                forKey:NSLocalizedDescriptionKey];

            NSError __autoreleasing *error = [[NSError alloc] initWithDomain:@"domain"

                code:1

                userInfo:userInfoDict];

            *outError = error;

        }

        return NO;

    }

    else

    {

        return YES;

    }

}

When this runs, I see that the ioValue has 0 length and that it returns NO, but then my completion handler is never called. Any help would be great.

Is there something I am missing for how to handle saving errors with UIManagedDocument - particularly how to notify the calling code that an error happened while saving its information.

2

There are 2 best solutions below

0
On BEST ANSWER

I guess I need to implement handleError:(NSError *)error userInteractionPermitted:(BOOL)userInteractionPermitted in a subclass of the UIManagedDocument. I found that via this article - http://blog.stevex.net/2011/12/uimanageddocument-autosave-troubleshooting/

0
On

As a rule, you should only call saveToURL to create a brand new file. Let auto-save do the rest.

Also, I'm not sure I follow your question. If you are asking how to know about save failures, the best you can do is register for notifications (since all saves happen on a background thread).

Directly from the documentation:

A UIDocument object has a specific state at any moment in its life cycle. You can check the current state by querying the documentState property. And you can be notified of changes in the state of a document by observing the UIDocumentStateChangedNotification notification.