本文介绍了Core数据在UIManagedDocument上的saveToURL上失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试设置核心数据,但是当我在 UIManagedDocument 上运行 saveToURL 创建它。这是我的代码;

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 首先被调用,但是当它被创建,我得到日志创建失败 - 我不明白,因为没有错误。有任何方法来获取错误消息吗?任何人都知道为什么它不工作?

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?

推荐答案

痛苦得到解除,我真的要开始阅读我的代码字母的信!我使用 NSDocumentationDirectory 而不是 NSDocumentDirectory

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

这篇关于Core数据在UIManagedDocument上的saveToURL上失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 14:49