• 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: [Core Data] Can we use setValue:forKey: in *willSave* method of NSManagedObject?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Core Data] Can we use setValue:forKey: in *willSave* method of NSManagedObject?


  • Subject: Re: [Core Data] Can we use setValue:forKey: in *willSave* method of NSManagedObject?
  • From: mmalc Crawford <email@hidden>
  • Date: Fri, 22 May 2009 01:03:50 -0700


On May 22, 2009, at 12:00 AM, Aurélien Hugelé wrote:

The willSave method is not very well described. In particular, can we use setValue:forKey: (or trigger change notifications) inside it?

It's not clear what setValue:forKey: has to do with it.
That apart, in general, the documentation strongly recommends against using KVC. Since you clearly already have a custom class, you should be using the accessor methods.



it seems that for *deleted* objects only, triggering change notifications is now allowed.
Am I right ?


No.
** The error message is telling you what your problem is **:

Failed to process pending changes before save. The context is still dirty after 100 attempts. ** Typically this recursive dirtying is caused by a bad validation method, -willSave, or notification handler. ** "


and a simple backtrace would probably have confirmed.

It's not clear why you regard this as "strange". Whatever you're doing in willSave is causing further change propagation (marking your object as dirty), which is resulting in willSave being invoked again, which is causing further change propagation...

One of the more common reasons for this is:

- (void)willSave {

	self.lastModifiedDate = [NSDate date];
	...

}

It doesn't matter whether you invoke accessor methods or setValue:forKey: within willSave -- you need to test to ensure that values haven't changed, or haven't changed by more than a certain amount, before modifying them so that you don't end up in an infinite loop.
An alternative strategy is to perform updates in response to an NSManagedObjectContextWillSaveNotification.


mmalc

_______________________________________________

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


References: 
 >[Core Data] Can we use setValue:forKey: in *willSave* method of NSManagedObject? (From: Aurélien Hugelé <email@hidden>)

  • Prev by Date: Re: Compiler does not synthesize KVO compliant properties for CATiledLayer subclass (was: Synthesized properties for scalars not KVO compliant)
  • Next by Date: entity name not found when model is in private framework
  • Previous by thread: [Core Data] Can we use setValue:forKey: in *willSave* method of NSManagedObject?
  • Next by thread: entity name not found when model is in private framework
  • Index(es):
    • Date
    • Thread