Re: Observer CoreData Objects changes
Re: Observer CoreData Objects changes
- Subject: Re: Observer CoreData Objects changes
- From: William Turner <email@hidden>
- Date: Sun, 27 Apr 2008 22:15:46 -0700
A few additional things to consider:
1) Avoid overriding validateValue:forKey:error (see "Property-Level
Validation" in http://developer.apple.com/documentation/Cocoa/Conceptual/CoreData/Articles/cdValidation.html)
2) Avoid overriding initWithEntity:insertIntoManagedObjectContext:
(see "Methods you Are Discouraged From Overriding" in http://developer.apple.com/documentation/Cocoa/Reference/CoreDataFramework/Classes/NSManagedObject_Class/Reference/NSManagedObject.html)
Some questions:
Do you need to observe all objects of a specific entity type?
Is the controller which needs to observe directly involved with
creation of the objects that need to be observed?
Will objects that were loaded from a previously saved store also
require observation?
You may want to implement a "commonAwake" method that is invoked from
both awakeFromInsert and awakeFromFetch.
Wil
On Apr 26, 2008, at 9:44 PM, Ben Lachman wrote:
I'm not completely clear on what you're wanting, but you might try
this:
•Have your controller observe the managed object context's
NSManagedObjectContextObjectsDidChangeNotification, the userinfo of
this note contains the key NSInsertedObjectsKey and
NSDeletedObjectsKey which should let you know which objects to start/
stop observing. Make sure you read the docs about this note and
when it's sent, etc.
•In your controller call addObserver:forKeyPath:... on each of the
newly added objects that you're interested in.
In general though it is easier to bind to stuff through a pre-built
controller that is in entity mode than observe all the objects
yourself. KVO is definitely the way to go though since it gives you
change info.
HTH,
->Ben
--
Ben Lachman
Acacia Tree Software
http://acaciatreesoftware.com
email: email@hidden
twitter: @benlachman
mobile: 740.590.0009
On Apr 26, 2008, at 6:03 PM, Dex Morgan wrote:
Hello,
I need to observe from a controller class when a particular
attribute of a NSManagedObject in my persistent store is changed.
I've tried with:
- (BOOL)validateValue:(id *)value forKey:(NSString *)key error:
(NSError **)error
in my class. Unfortunatly I need to keep the old value in order to
make a comparison between it and the new value (id* value) and this
method does not allow it.
So I've tried using
- (void) addObserver:<#(NSObject *)observer#>
forKeyPath:<#(NSString *)keyPath#>
options:<#(NSKeyValueObservingOptions)options#> context:<#(void
*)context#>
but the only way to keep changes of all objects is to put this
method into the init of my class (inside a subclassed method -
(id)initWithEntity:(NSEntityDescription *)entity
insertIntoManagedObjectContext:(NSManagedObjectContext *)context).
Bleah, it's not a great solution right?
Anyone can point to me to the right way?
Thanks in advanced
_______________________________________________
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
_______________________________________________
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
_______________________________________________
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