HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

How best to keep track of an unknown number of objects?

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
numbertrackobjectsunknownkeephowbest

Problem

I have a manager class that will manage an unknown number of objects. I'm currently storing the objects in a NSMutableArray object, and when I need to find one, I iterate through the array comparing pointers.

For example, I have an array called managedObjects. I get a delegate method call:

- (void)someDelegateMethod:sender withResults:results {
    for (NSDictionary *managedObjectDict in managedObjects) {
        if (sender == [managedObjectDict objectForKey:@"DelegatedObject"]) {
            NSLog(@"%@",[managedObjectDict objectForKey:@"Value"]);
            break;
        }
    }
}


Now, Objective-C forin loops are handled in batches and are extremely fast... but if I were managing thousands of objects, this could potentially take a bit of time to find the right one.

How can I improve this efficiency?

Solution

I'm not an Objective-C guy but, I do know that if you used some sort of hash table structure and could define a hashcode for these objects, that you'd be able to pull them out much faster.

You said at times you "need to find one", I would assume you would know enough about it to be able to generate its hashcode and check your table for it in O(1) time.

Context

StackExchange Code Review Q#45054, answer score: 4

Revisions (0)

No revisions yet.