问题描述
的访问, ?
根据, NSManagedObject
保留对其 NSManagedObjectContext
在内部,我们可以访问它。
Zarra如何知道这一点,
$ b
我问,因为我想知道 NSManagedObjectContext
是否 dealloc
在 tearDown
方法中。 (我使用。)
#import< SenTestingKit / SenTestingKit.h>
#import< CocoaPlant / CocoaPlant.h>
#importAccountUser.h
@interface AccountUserTests:SenTestCase {
AccountUser * accountUser;
}
@end
@implementation AccountUserTests
- (void)setUp {
accountUser = [AccountUser insertIntoManagedObjectContext:
[NSManagedObjectContext contextWithStoreType:NSInMemoryStoreType error:NULL]];
}
- (void)tearDown {
[accountUser delete];
}
- (void)testFetchWithLinkedAccountUserID {
//测试到这里...
}
@end
NSManagedObject不会强制引用其NSManagedObjectContext。
我检查了一个测试项目。
因此,只要你使用它的对象,你应该保持对NSManagedObjectContext的强引用。
NSManagedObject
provides access to its NSManagedObjectContext
, but does it retain it?
According to "Passing Around a NSManagedObjectContext on iOS" by Marcus Zarra, "The NSManagedObject
retains a reference to its NSManagedObjectContext
internally and we can access it."
How does Zarra know this and is he correct?
I'm asking because I want to know if the NSManagedObjectContext
will be dealloc
'ed in the tearDown
method below. (I'm using CocoaPlant.)
#import <SenTestingKit/SenTestingKit.h>
#import <CocoaPlant/CocoaPlant.h>
#import "AccountUser.h"
@interface AccountUserTests : SenTestCase {
AccountUser *accountUser;
}
@end
@implementation AccountUserTests
- (void)setUp {
accountUser = [AccountUser insertIntoManagedObjectContext:
[NSManagedObjectContext contextWithStoreType:NSInMemoryStoreType error:NULL]];
}
- (void)tearDown {
[accountUser delete];
}
- (void)testFetchWithLinkedAccountUserID {
// Tests go here...
}
@end
解决方案 NSManagedObject DOES NOT hold strong reference to its NSManagedObjectContext. I've checked that on a test project.Therefore, you should keep strong reference to NSManagedObjectContext as long as you use its objects.
这篇关于NSManagedObject是否保留其NSManagedObjectContext?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!