• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Why does Core Data retain an object when one of that object's attributes is changed?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Why does Core Data retain an object when one of that object's attributes is changed?


  • Subject: Re: Why does Core Data retain an object when one of that object's attributes is changed?
  • From: Ron Lue-Sang <email@hidden>
  • Date: Thu, 14 Aug 2008 12:03:24 -0700

Two words. Un Do.

The changes you're making to the managedObject are being recorded for undo. The undo action which coredata puts together behind the scenes for you needs to make sure that the object that will be undone lives for as long as the undo action does. Clear the undo stack or undo and you'll magically see the undo count go back down. You can also turn off undo completely by setting the context's undo manager to nil.

You usually don't want to turn off undo tho. Sometimes you want to temporarily disable undo so that changes you make to the object aren't visible to the user or undoable. But that's a different topic.

On Aug 14, 2008, at 11:21 AM, Marco Masser wrote:

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 (email@hidden)

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:
This email sent to email@hidden


--------------------------
RONZILLA



_______________________________________________

Cocoa-dev mailing list (email@hidden)

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:
This email sent to email@hidden


  • Follow-Ups:
    • Re: Why does Core Data retain an object when one of that object's attributes is changed?
      • From: Marco Masser <email@hidden>
References: 
 >Why does Core Data retain an object when one of that object's attributes is changed? (From: Marco Masser <email@hidden>)

  • Prev by Date: Re: @try @catch
  • Next by Date: Re: Is this a bug, or am I hacking?
  • Previous by thread: Why does Core Data retain an object when one of that object's attributes is changed?
  • Next by thread: Re: Why does Core Data retain an object when one of that object's attributes is changed?
  • Index(es):
    • Date
    • Thread