NSManagedObject - NSSet gets removed?
I have an nsmanagedObject this NSManagedObject contains an NSSet.
the data for NSSet get's lost when i call release on an NSManagedObject with retain count of 2.
Wouldn't retaining an NSManagedO开发者_StackOverflow社区bject also retain all it's properties??
- (id)initViewWithManagedObject :(NSManagedObject)obj
{
if (self = [super init])
{
self.managedObject = obj;
}
return self;
}
- (void)dealloc
{
self.managedObject = nil;
//Here is when the nsset data gets removed
[super dealloc];
}
Below describes how the property was created
@interface MyManagedObject :NSManagedObject
@property (nonatomic, retain) NSSet *mySet;
@end
@implementation MyManagedObject
@dynamic mySet;
@end
Why are you calling [self.managedObject release]
? You should never call -release
on the results of calling a getter. The appropriate code here is simply [managedObject release]
(assuming managedObject
is the name of the backing ivar).
Furthermore, once you release the object, why are you inspecting its properties?
Is it safe to assume your application is getting an NSSet which you want to be a Core Data relationship?
In the case, the problem isn't around releasing and retaining. The problem is you aren't saving the managed object context before you're done with the object, so when you fetch again, the data is lost.
Here is the solution to the problem: I had to create both way relationship from object a to b and from object b to a. For example:
CountryManagedObject *country = [Factory getCountryByName:@"USA"];
StateManagedObject *state = [Factory createStateByName:@"California"];
[country.statesSet addObject:state]; //assuming NSSet responds to addObject
//the line below fixed the problem
//State has a reversed relationship to country named "country"
state.country = country;
精彩评论