3

我正在尝试设置核心数据,但是当我在其上运行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首先被调用,但是当它被创建时,我得到日志“创建失败” - 我不明白,因为没有错误。有什么方法可以获取错误信息吗?有谁知道为什么它不起作用?

4

1 回答 1

18

痛得解脱,我真的要开始逐字阅读我的密码了!我使用的是NSDocumentationDirectory而不是NSDocumentDirectory

于 2012-08-22T08:33:56.533 回答