UIManagedDocument上的saveToURL上的核心数据失败

时间:2012-08-21 19:46:57

标签: objective-c ios core-data uimanageddocument

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

@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,但是当它被创建时,我得到日志“创建失败” - 我不明白,因为没有错误。有没有办法得到错误信息?有谁知道它为什么不起作用?

1 个答案:

答案 0 :(得分:18)

痛苦得到缓解,我真的必须开始阅读我的密码来写信!我使用的是NSDocumentationDirectory而不是NSDocumentDirectory