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