Hi!

I came across a strange problem today:

I have a simple Core Data Application whose managed object model has one entity with one attribute. I set up an AppController class that inserts one instance of that entity's backing class (NSManagedObject) into the MOC:

- (void)awakeFromNib {
myObject = [NSEntityDescription insertNewObjectForEntityForName:@"MyEntity" inManagedObjectContext: [[NSApp delegate] managedObjectContext]];
}


Then, there is an IBAction that is called every time I press a button in the GUI:

- (IBAction)doIt:(id)sender {

        NSLog(@"before: object's retain count: %i", [myObject retainCount]);
        
// Call the setter of myBoolAttribute. This will change object's retain count [myObject setValue:[NSNumber numberWithBool:YES] forKey:@"myBoolAttribute"];

NSLog(@"after : objects's retain count: %i\n\n", [myObject retainCount]);
}


The strange thing is that setting the myBoolAttribute increases object's retain count every time. I tried doing a [managedObjectContext setRetainsRegisteredObjects:NO]; in the Xcode template's AppDelegate class, right after the managed object context is created (line 93, the line right after alloc+init), but it just makes the first value printed by the above method start lower by one (as expected).


I did nothing more than the above. No special settings for the entity or the class, nothing. Then, I created an NSManagedObject subclass for testing different, self-implemented setters for myAttribute:

@interface MyEntityClass : NSManagedObject { }
@property(retain, nonatomic) NSNumber *myBoolAttribute;
@end

@implementation MyEntityClass

@dynamic myBoolAttribute;

// 1. Using Core Data's generated setter causes self's retain count to be changed

// 2. Using this setter causes self's retain count to be changed
- (void)setMyBoolAttribute:(NSNumber *)value {
        [self willChangeValueForKey:@"myBoolAttribute"];
        [self setPrimitiveValue:value forKey:@"myBoolAttribute"];
        [self didChangeValueForKey:@"myBoolAttribute"];
}

// 3. Using this setter does not cause self's retain count to be changed - but it's wrong
- (void)setMyBoolAttribute:(NSNumber *)value {
        [self setPrimitiveValue:value forKey:@"myBoolAttribute"];
}

// 4. This setter is stupid and should cause a crash later on - but it doesn't
- (void)setMyBoolAttribute:(NSNumber *)value {
        [self willChangeValueForKey:@"myBoolAttribute"];
        [self setPrimitiveValue:value forKey:@"myBoolAttribute"];
        [self didChangeValueForKey:@"myBoolAttribute"];
        [self release];
}

@end


After a few NSLog()'s in setter #3 it is clear, that - willChangeValueForKey: increases the retain count. Why does it do that?


After that, I tried adding additional attributes to my entity and to make everything even worse, *all* attributes' retain counts are increased by -willChangeValueForKey:


This is not the first time I'm writing a Core Data application and I just can't see where the problem lies. I was really thinking long and hard about this before I wrote this mail, but it just doesn't make sense to me...

Any comments are greatly appreciated!

Marco
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to