【发布时间】:2011-04-06 23:10:16
【问题描述】:
我正在为一个更大的 iOS 应用程序编写一个 Core Data ContextManager。 ContextManager 提供了一个 NSManagedContext,当其他 ContextManager 将它们的 NSMangedContext 保存到持久数据存储时,该 NSManagedContext 会自动更新。
我有一个单元测试 (TestContextManager),它创建两个上下文,将一个对象添加到一个上下文中,并测试该对象是否出现在另一个上下文中。它没有。 为什么最后一次测试失败了?
这是 ContextManager 和失败的单元测试的代码。单元测试中的最后一个断言失败。其他所有断言都通过。如您所见,ContextManager 依赖于从不同的 ContextManager 获取更改通知并使用mergeChangesFromContextDidSaveNotification 进行自我更新。请注意,此测试的所有内容都发生在同一个线程上。
我知道 NSManagedObjectContextDidSaveNotification 正在正确发送和接收。我知道 NSManagedObjectContextDidSaveNotification 在其 userInfo 字典中有正确的数据。
我还在使用 SQLite 持久存储的实际设备上将这个单元测试作为应用程序测试运行——同样的断言失败。
提前致谢!
上下文管理器:
#import "ContextManager.h"
@implementation ContextManager
@synthesize context;
#pragma mark - Custom code
- (void)save {
NSError *error = nil;
if (self.context != nil) {
if ([self.context hasChanges] && ![self.context save:&error]) {
NSAssert1(FALSE, @"Unable to save the managed object context. UserInfo:\n%@", [error userInfo]);
}
}
return;
}
- (void)mergeChanges:(NSNotification *)notification {
if (notification.object != self.context) {
[self.context mergeChangesFromContextDidSaveNotification:notification];
}
return;
}
#pragma mark - Overridden NSObject methods
#pragma mark Creating, copying, and deallocating object
- (id)initWithPersistentStoreCoordinator:(NSPersistentStoreCoordinator *)persistentStoreCoordinator {
self = [super init];
if (self) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(mergeChanges:) name:NSManagedObjectContextDidSaveNotification object:nil];
self.context = [[[NSManagedObjectContext alloc] init] autorelease];
[self.context setPersistentStoreCoordinator:persistentStoreCoordinator];
}
return self;
}
- (void)dealloc {
[context release];
[super dealloc];
return;
}
@end
TestContextManager:
#import "TestContextManager.h"
#import "ContextManager.h"
#import "CoreDataManager.h"
#define TEST_MANAGED_OBJECT @"AManagedObject"
@implementation TestContextManager
- (void)testContextManager {
CoreDataManager *coreDataManager = [[CoreDataManager alloc] init];
coreDataManager.storeType = NSInMemoryStoreType;
ContextManager *contextManagerA = [coreDataManager provideContextManager];
if (!contextManagerA) STFail(@"CoreDataManager did not provide a context manager.");
NSManagedObjectContext *contextA = contextManagerA.context;
if (!contextA) STFail(@"ContextManager did not provide a managed object context.");
// setA1 has 0 objects (or whatever is initially there).
NSSet *setA1 = [contextManagerA.context registeredObjects];
[NSEntityDescription insertNewObjectForEntityForName:TEST_MANAGED_OBJECT inManagedObjectContext:contextManagerA.context];
// setA2 has 1 object.
NSSet *setA2 = [contextManagerA.context registeredObjects];
STAssertTrue([setA2 count] == [setA1 count]+1, @"Context provided by ContextManager is not accepting new objects.");
[contextManagerA save];
ContextManager *contextManagerB = [coreDataManager provideContextManager];
[NSEntityDescription insertNewObjectForEntityForName:TEST_MANAGED_OBJECT inManagedObjectContext:contextManagerB.context];
[contextManagerB save];
NSSet *setA3 = [contextManagerA.context registeredObjects];
// setA3 should have 2 objects <=== THIS TEST FAILS
STAssertTrue([setA3 count] == [setA1 count]+2, @"Context is not updating new objects.");
[coreDataManager release];
return;
}
@end
【问题讨论】:
标签: objective-c cocoa xcode ios core-data