我正在尝试设置核心数据,但是当我在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
,但是当它被创建时,我得到日志“创建失败” - 我不明白,因为没有错误。有没有办法得到错误信息?有谁知道它为什么不起作用?
答案 0 :(得分:18)
痛苦得到缓解,我真的必须开始阅读我的密码来写信!我使用的是NSDocumentationDirectory
而不是NSDocumentDirectory