我不想进入很长的答案,因为我没有使用MagicalRecord,而且我也没有IDEA它如何管理模型配置。
这就是说,你想要解决这个问题的方式是使用模型配置和多个存储文件。这个问题既好理解又有据可查。
Apple's documentation是一个很好的起点,并且有numerous articles and examples on the web。
EDIT
行DAN,这里是用于使用多个配置有点做作(但简单)的例子。您应该能够将其复制/粘贴到测试文件中并运行它,这应该允许您追踪正在发生的事情并获得基本的理解。
请注意,这不是我会建议编写生产代码或测试的方式(我也不建议忽略错误),但我希望这有助于解释一些事情,并允许您进行试验。
我把代码分解成了几个辅助方法,希望能更好地解释。
首先,我们创建一个简单模型,其中包含四个实体,我们将在这两个实体中放入两个参数。
- (NSManagedObjectModel *)makeConfigurationModel {
NSAttributeDescription *nameAttr = [[NSAttributeDescription alloc] init];
nameAttr.name = @"name";
nameAttr.attributeType = NSStringAttributeType;
NSEntityDescription *foo = [[NSEntityDescription alloc] init];
foo.name = @"Foo";
foo.properties = @[[nameAttr copy]];
NSEntityDescription *bar = [[NSEntityDescription alloc] init];
bar.name = @"Bar";
bar.properties = @[[nameAttr copy]];
NSEntityDescription *blarg = [[NSEntityDescription alloc] init];
blarg.name = @"Blarg";
blarg.properties = @[[nameAttr copy]];
NSEntityDescription *baz = [[NSEntityDescription alloc] init];
baz.name = @"Baz";
baz.properties = @[[nameAttr copy]];
NSManagedObjectModel *model = [[NSManagedObjectModel alloc] init];
model.entities = @[foo, bar, blarg, baz];
[model setEntities:@[foo, bar] forConfiguration:@"One"];
[model setEntities:@[blarg, baz] forConfiguration:@"Two"];
return model;
}
接下来,一个函数将两个商店分配给PSC,并创建一些示例实体。该功能还检查以确保可以访问所有实体。
- (void)setupDatabaseWithModel:(NSManagedObjectModel*)model
store1:(NSURL*)store1URL
store2:(NSURL*)store2URL {
@autoreleasepool {
NSPersistentStoreCoordinator *psc = [[NSPersistentStoreCoordinator alloc]
initWithManagedObjectModel:model];
[psc addPersistentStoreWithType:NSSQLiteStoreType
configuration:@"One"
URL:store1URL
options:nil
error:NULL];
[psc addPersistentStoreWithType:NSSQLiteStoreType
configuration:@"Two"
URL:store2URL
options:nil
error:NULL];
NSManagedObjectContext *moc = [[NSManagedObjectContext alloc]
initWithConcurrencyType:NSMainQueueConcurrencyType];
moc.persistentStoreCoordinator = psc;
// Add some entities...
NSArray *entityNames = @[@"Foo", @"Bar", @"Blarg", @"Baz"];
for (NSString *e in entityNames) {
NSManagedObject *obj =
[NSEntityDescription insertNewObjectForEntityForName:e
inManagedObjectContext:moc];
[obj setValue:[NSString stringWithFormat:@"%@ 1", e] forKey:@"name"];
}
[moc save:NULL];
// Should have all of them in this MOC...
for (NSString *e in entityNames) {
NSFetchRequest *fetchRequest = [NSFetchRequest
fetchRequestWithEntityName:e];
NSArray *result = [moc executeFetchRequest:fetchRequest error:NULL];
XCTAssertEqual(1, result.count);
NSManagedObject *obj = [result firstObject];
XCTAssertEqualObjects(([NSString stringWithFormat:@"%@ 1", e]),
[obj valueForKey:@"name"]);
}
}
}
还有一个函数来检查某些实体是否在商店中(或不在)。
- (void)checkStore:(NSURL*)storeURL
model:(NSManagedObjectModel*)model
present:(NSArray*)present
notPresent:(NSArray*)notPresent {
@autoreleasepool {
NSPersistentStoreCoordinator *psc = [[NSPersistentStoreCoordinator alloc]
initWithManagedObjectModel:model];
[psc addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil
URL:storeURL
options:nil
error:NULL];
NSManagedObjectContext *moc = [[NSManagedObjectContext alloc]
initWithConcurrencyType:NSMainQueueConcurrencyType];
moc.persistentStoreCoordinator = psc;
for (NSString *e in present) {
NSFetchRequest *fetchRequest = [NSFetchRequest
fetchRequestWithEntityName:e];
NSArray *result = [moc executeFetchRequest:fetchRequest error:NULL];
XCTAssertEqual(1, result.count);
NSManagedObject *obj = [result firstObject];
XCTAssertEqualObjects(([NSString stringWithFormat:@"%@ 1", e]),
[obj valueForKey:@"name"]);
}
for (NSString *e in notPresent) {
NSFetchRequest *fetchRequest = [NSFetchRequest
fetchRequestWithEntityName:e];
NSArray *result = [moc executeFetchRequest:fetchRequest error:NULL];
XCTAssertEqual(0, result.count);
}
}
}
而一个小帮手删除URL
static void removeURL(NSURL ** url) {
[[NSFileManager defaultManager] removeItemAtURL:*url error:NULL];
}
和测试功能...
- (void)testConfigurations {
__attribute__((cleanup(removeURL))) NSURL * __autoreleasing dirURL =
[[[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory
inDomain:NSUserDomainMask
appropriateForURL:nil
create:YES
error:NULL]
URLByAppendingPathComponent:[[NSUUID UUID] UUIDString]];
[[NSFileManager defaultManager] createDirectoryAtURL:dirURL
withIntermediateDirectories:YES
attributes:nil
error:NULL];
NSManagedObjectModel *model = [self makeConfigurationModel];
NSURL *store1URL = [dirURL URLByAppendingPathComponent:@"store1"];
NSURL *store2URL = [dirURL URLByAppendingPathComponent:@"store2"];
[self setupDatabaseWithModel:model store1:store1URL store2:store2URL];
[self checkStore:store1URL
model:model
present:@[@"Foo", @"Bar"]
notPresent:@[@"Blarg", @"Baz"]];
[self checkStore:store2URL
model:model
present:@[@"Blarg", @"Baz"]
notPresent:@[@"Foo", @"Bar"]];
}
这是什么型号的配置都是。 –
他们应该如何使用?这是一个类似于我的场景:http://blog.atwam.com/blog/2012/05/11/multiple-persistent-stores-and-seed-data-with-core-data其中建议使用两种模式合并到一个NSPersistentStoreCoordinator中。 – DAN
我们中的一个人错过了一些东西。 “种子”文件应该只包含Foo实体。当你说它包含“空”实体时,你是什么意思?是否有一堆具有默认值或实体的实体?如果是这样,也许你的设置不正确。你怎么知道在“种子”文件中有Bar,Blarg和Baz实体?你如何获得它你从“种子”数据库中获取所有“酒吧”实体?你确定你不只是混淆了模型定义每个实体的事实,而且数据库中没有实际的实体吗? –