我在应用程序中遇到了问题,CoreData的工作方式与应用程序在模拟器中一样 - 但不在设备上。CoreData“验证存储的URL的错误”
我收到
2010-09-30 12:45:07.500 CoreDataTutorial_iOS[130:307] Unresolved error Error Domain=NSCocoaErrorDomain Code=513 "The operation couldn’t be completed. (Cocoa error 513.)" UserInfo=0x1412a0 {NSUnderlyingException=Error validating url for store}, {
NSUnderlyingException = "Error validating url for store";
我呼吁PersistentStoreCoordinator在这个函数(会抛出上述错误):
-(NSPersistentStoreCoordinator*)persistentStoreCoordinator
{
if(persistentStoreCoordinator_ != nil)
return persistentStoreCoordinator_;
NSURL *aStoreURL = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingFormat:@"corebase.sqlite"]];
NSError *anError = nil;
persistentStoreCoordinator_ = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if(![persistentStoreCoordinator_ addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:aStoreURL options:nil error:&anError])
{
NSLog(@"Unresolved error %@, %@", anError, [anError userInfo]);
abort();
}
return persistentStoreCoordinator;
}
我设置一个破发点,在“objc_exception_throw “,以查看aStoreURL是什么,它是: file://localhost/var/mobile/Applications/BE9A2982-BDC3-405D-A201-FB78E9E0790B/Documentscorebase.sqlite
我注意到它本身并没有在“/ Documents”之后添加最后的“/”。 当我创建了一个URL这样
NSURL *aStoreURL = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingFormat:@"/corebase.sqlite"]];
这似乎已经奏效,或者至少得以通过的那部分。 该功能不应该附加该部分本身吗?
-(NSString*) applicationDocumentsDirectory
{
return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
}
它在模拟器中正常工作,什么是正确的做法?