我正在尝试向 Core Data 应用程序添加一些测试。该应用程序实际上运行良好,当我尝试创建堆栈时,测试失败得很惨。
尝试向以下类型添加类型时NSPersistentStore
出现SQLite
错误NSPersistentStoreCoordinator
:
- (NSPersistentStoreCoordinator *)storeCoordinator
{
if (_storeCoordinator == nil) {
_storeCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:self.model];
NSError *err = nil;
if (![_storeCoordinator addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil
URL:self.dbURL
options:[[self class] persistentStoreCordinatorOptions]
error:&err]) {
//This is where I get the error
NSNotification *note = [NSNotification
notificationWithName:[[self class] persistentStoreCoordinatorErrorNotificationName]
object:self
userInfo:@{@"error" : err}];
[[NSNotificationCenter defaultCenter] postNotification:note];
NSLog(@"Error while adding a Store: %@", err);
return nil;
}
}
return _storeCoordinator;
}
这是错误:
CoreData: error: -addPersistentStoreWithType:SQLite configuration:(null) URL:file:///Users/cfisher/Library/Developer/CoreSimulator/Devices/860C8F97-354D-4A9D-B1E9-CC018680F487/data/Library/Caches/TestModel options:{
NSInferMappingModelAutomaticallyOption = 1;
NSMigratePersistentStoresAutomaticallyOption = 1;
NSReadOnlyPersistentStoreOption = 1;
} ... returned error Error Domain=NSCocoaErrorDomain Code=260 "The operation couldn’t be completed. (Cocoa error 260.)" with userInfo dictionary {
}
错误中提到的 URL 是通过以下方式获取的(下面的 dbURL 属性):
- (void)setUp {
[super setUp];
NSURL *cache = [[[NSFileManager defaultManager]
URLsForDirectory:NSCachesDirectory
inDomains:NSUserDomainMask] lastObject];
self.dbURL = [cache URLByAppendingPathComponent:self.testModelName];
self.testModelName = @"TestModel";
self.testBundle = [NSBundle bundleForClass:[self class]];
}
这只发生在测试中。该应用程序运行良好。有任何想法吗?