Re: CD: Creating a managed object
Re: CD: Creating a managed object
- Subject: Re: CD: Creating a managed object
- From: Ian Joyner <email@hidden>
- Date: Thu, 31 May 2007 11:25:02 +1000
Reviewing this original question and after some thoughts from Mr
Crawford, I think it is more about bindings and the NSController
architecture (no surprise there), rather than Core Data.
With Cocoa bindings, you just connect up the bindings in IB and
messages just get passed to the right things and it works. The great
thing about Cocoa bindings is you don't have to write all that messy
code that you normally have to in the controller part (thanks to KVC/
O). However, what I needed to do was to record something that
happened in the view in the CD model entities (this isn't your
typical IT app). (It's actually a bit like a game, but user actions
are recorded, hence use of CD.)
I didn't like putting manipulation of a CD entity in the view, but
using bindings, the controller bit seemed rather closed off. So the
question is how to get controller code into the bindings scheme of
things.
It occurred to me that maybe I could subclass NSArrayController, not
necessarily an appealing thought (hadn't seen it done anywhere else),
and Marcus Zarra didn't like it either (thank you).
So I think the approach is to write your own controller class,
interact with the configured NSArrayController class, and connect to
this specific class from the custom view class (perhaps as a
delegate, although that might be overkill for this simple situation).
So you have:
NSView NSObject (NSController????)
^ ^
| |
MyView ----------------> MyController ------------------->
NSArrayController
mouseDown: ---------> create_point --------------------> newObject
set values
(maybe done by setting MyController as a
delegate)
Does that look reasonable in the MVC/Cocoa bindings scheme of things?
Thanks
Ian
On 28/05/2007, at 4:01 PM, Ian Joyner wrote:
I have a simple app with a view with a mouse down handler. The
simple core data model has an entity to track where the user has
clicked. The basic architecture I have come up with is to add an
outlet to the view for the NSArrayController controlling the point
entities. This outlet is connected in IB to the controller. When
the view is clicked mouseDown: is called:
IBOutlet NSArrayController *nsa;
.....
-(void) mouseDown: (NSEvent *)event {
[nsa insert: self];
}
Is the view holding a reference to the controller a reasonable
architecture? I'm kind of doing what a plus button would do to send
add: to the controller to create a new object.
If this is OK, the next step is to change the insert to an
addObject. Getting code out of the Core Data Programming Guide I'd
do something like:
-(void) mouseDown: (NSEvent *)event {
NSManagedObject *point = [NSEntityDescription
insertNewObjectForEntityForName: @"Point" inManagedObjectContext:
context]; // Where do I get 'context'?
[point setX: event.x]; // pseudo code
[point setY: event.y];
[nsa addObject: point];
}
Problem I have with the above is that we are in a view and the
managed object context belongs to the document, which the view does
not know about the context, which makes me think there is something
wrong with this architecture.
So is there a better way of creating a managed object when a click
is received in a view? Or if this is a legitimate way of doing it,
how do I get the right context to insert the managed object into?
Thanks
Ian Joyner
Sportstec
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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