Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does an NSManagedObject retain its 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
like image 477
ma11hew28 Avatar asked Dec 04 '11 20:12

ma11hew28


1 Answers

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.

like image 125
eDeniska Avatar answered Sep 22 '22 16:09

eDeniska