3

I'm trying to set up core data but when i'm running saveToURL on the UIManagedDocument it's failing to create it. This is my code;

@property (nonatomic, strong) UIManagedDocument *currentUserDatabase;

@synthesize currentUserDatabase = _currentUserDatabase;

- (void)setCurrentUserDatabase:(UIManagedDocument *)currentUserDatabase
{
    _currentUserDatabase = currentUserDatabase;
    [self useDocument];
} 


- (void)isUserLoggedIn
{
    if (!self.currentUserDatabase) {
        NSURL *url = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentationDirectory inDomains:NSUserDomainMask] lastObject];
        url = [url URLByAppendingPathComponent:@"Current User Database"];
        self.currentUserDatabase = [[UIManagedDocument alloc] initWithFileURL:url];
    }
}

- (void)useDocument
{
    if (![[NSFileManager defaultManager] fileExistsAtPath:[self.currentUserDatabase.fileURL path]]) {
        [self.currentUserDatabase saveToURL:self.currentUserDatabase.fileURL forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success) {
            if (success) {
                NSLog(@"create success");
            } else {
                NSLog(@"create failed");
            }
            //[self getCurrentUser];
        }];
    } else if (self.currentUserDatabase.documentState == UIDocumentStateClosed) {
        [self.currentUserDatabase openWithCompletionHandler:^(BOOL success) {
            //[self getCurrentUser];
        }];
    } else if (self.currentUserDatabase.documentState == UIDocumentStateNormal) {
        //[self getCurrentUser];
    }
}

isUserLoggedIn gets called first, but when it gets created, I get the log "create failed" - I don't understand because there is no errors. Are there any ways to get error messages? Anyone know why it's not working?

1 Answer 1

18

Pain is relieved, I really have to start reading my code letter for letter! I was using the NSDocumentationDirectory instead of the NSDocumentDirectory

1
  • Wow, I did the same!! I watched Paul Hegarty too, wow! Good to know I'm not alone. This later resulted in no persistent store error. Jul 3, 2013 at 18:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.