Re: Inserting objects in content array
Re: Inserting objects in content array
- Subject: Re: Inserting objects in content array
- From: mmalcolm crawford <email@hidden>
- Date: Wed, 12 May 2004 17:04:32 -0700
On May 12, 2004, at 2:47 PM, Fritz Anderson wrote:
If it were me, I'd consider the adding of the records to the array to
be an operation on the model, and use methods for altering the array
_in the model object_ that would be observable under Key-Value
Observation: Make the changes to the result of
mutableArrayValueForKey:, or make direct changes and bracket them with
will/didChangeValueForKey: or will/didChange:valuesAtIndexes:forKey:.
I would guess that that would be unnecessarily inefficient -- you're
adding a level of indirection with the array proxy, and will get more
update events than you probably want.
I have an application that retrieves records from a database, then
stuffs them into an array that is managed by an NSArrayController.
Currently, I put all of the returned records into the array myself,
then call [arrayController rearrangeObjects] to have the updated
content array displayed. This seems to be working fine, but would it
be better/faster to use [arrayController addObject:aRecord] instead?
If you're getting back an array of objects from the database, I'd be
tempted to add them in a batch using something like this (assumes an
array 'employees' as an attribute of the array's container):
- (void) addObjectsToEmployeesFromArray: (NSArray *)otherArray
{
if ([otherArray count] > 0)
{
NSRange range = NSMakeRange([self countOfEmployees], [otherArray
count]);
NSIndexSet *indexes = [NSIndexSet indexSetWithIndexesInRange:range];
[self willChange:NSKeyValueChangeInsertion valuesAtIndexes:indexes
forKey:@"employees"];
[[self employees] addObjectsFromArray:otherArray];
[self didChange:NSKeyValueChangeInsertion valuesAtIndexes:indexes
forKey:@"employees"];
}
}
mmalc
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.