maddy
maddy

Reputation: 4111

search in an nsarray of nsdictionaries performance memory optimised way

this question is based on performance, i am getting desired results. i have a array of dictionaries like this:

Printing description of arrAppointmentDictionary:
<__NSArrayM 0x16f962a0>(
{
"component_id" = 159;
total = 1;
},
{
"component_id" = 165;
total = 1;
},
{
"component_id" = 177;
total = 1;
},
{
"component_id" = 191;
total = 1;
},
{
"component_id" = 193;
total = 1;
}
)

i searched in dictionary based on keys like this:

for (int i = 0; i<arrAppointmentDictionary.count; i++)
{
    NSMutableDictionary *appointmentDictionary = [arrAppointmentDictionary objectAtIndex:i];
    NSArray *keys = [appointmentDictionary allKeys];
    for (NSString *key in keys)
    {
        @autoreleasepool {
            NSLog(@"Key is %@", key);
            if([[appointmentDictionary objectForKey: key] isEqualToString:[rs stringForColumn:@"id"]])
            {
                layout.numberOfAppointments = [appointmentDictionary objectForKey: @"total"];
                NSLog(@"number of appointments are >>>>>>>>>>>>>>>>> %@", [appointmentDictionary objectForKey: @"total"]);
            }
        }
    }
}

i get the results accurate. how to increase performance/memory optimisations of this for loop as it is called from another while loop.

thanks & regards.

Upvotes: 0

Views: 36

Answers (1)

trojanfoe
trojanfoe

Reputation: 122468

Here's one way:

NSString *wantedId = [rs stringForColumn:@"id"];
for (NSMutableDictionary *appointmentDictionary in arrAppointmentDictionary) {
    if ([appointmentDictionary[@"id"] isEqualToString:wantedId]) {
        layout.numberOfAppointments = appointmentDictionary[@"total"];
        NSLog(@"number of appointments are >>>>>>>>>>>>>>>>> %@", appointmentDictionary[@"total"]);
        break;
    }
}

Upvotes: 1

Related Questions