On Mar 25, 2010, at 08:36, Gustavo Pizano wrote:
> for(ItemXInvoice * acutalItem in [_itemsArrayController
> arrangedObjects]){
> [acutalItem setValue:[acutalItem
> valueForKeyPath:@"toItem.unitPrice"] forKey:@"creationItemPrice"];
> [acutalItem setValue:[self getNewStatusEntity]
> forKey:@"toStatus"];
> }
>
> [_newInvoice addToItemsXInvoice:[NSSet
> setWithArray:[_itemsArrayController arrangedObjects]]];
[snip]
> NSArrayController * ctr = [(InvoiceLayoutViewController *)object
> _invoicesArrayController];
>
> NSSet * temp = [(Invoice *)[[ctr selectedObjects] lastObject]
> valueForKey:@"toItemsXInvoice"];
> NSLog(@"Changed selection %i",[temp count]);
The short answer:
Probably, at the time your 'for' loop executes, [_itemsArrayController
arrangedObjects] hasn't been rearranged yet, so you're looping through an
out-of-date array that doesn't contain the new objects yet.
The long answer:
This is not a good approach to MVC programming. You're trying to take short
cuts by updating your data model "by remote control" -- that is, by programming
NSArrayControllers instead of your data model. That's a bit like trying to
disassemble a precision watch using a crowbar and a hammer. :)
Array controllers are "glue" objects that are supposed to ease the task of
hooking up your user interface, but things get very difficult very quickly when
you try to use them as data model proxies. They have a lot going on internally,
and you have no real control over when internal things happen.
Asking an array controller for its selection is OK, although there are better
ways, such as creating a "selectionIndexes" property in your window controller
(or whatever object is functioning as File's Owner) and binding the array
controller's "selectionIndexes" binding to that property.
But adding objects, setting relationships and modifying object properties (for
the objects in your data model, which are Core Data managed objects in this
case) is best done directly, using KVC accessors on the objects themselves.
Here's the rule of thumb I use: any line of code that refers to an array
controller is a *last resort*, not a preferred solution, to be used only when
there is no alternative.
_______________________________________________
Cocoa-dev mailing list ([email protected])
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]