Balram Tiwari
Balram Tiwari

Reputation: 5667

A better logic for filtering based on NSDate

I have this logic of filtering my data (an array of dictionary, where dictionary have some date key & value). I feel that this is not a great solution using loop.

I need some key ideas so that I can enhance this solution & make is fast.

-(NSArray *)getCurrentShiftData {

    NSMutableArray *returnArray = [[NSMutableArray alloc]init];
    NSArray *tempArray = [[self dataStore] valueForKeyPath:key_RootKey];
    for(NSDictionary *dict in tempArray) {

        NSDateFormatter *empDateFormatter = [[NSDateFormatter alloc]init];
        [empDateFormatter setDateFormat:@"yyyy/M/d H:m"];
        NSString *dt = [dict valueForKey:key_StartDate];
        NSDate *empDate = [empDateFormatter dateFromString:dt];

        [empDateFormatter setDateFormat:@"yyyy/M/d"];
        NSString *compareDate = [empDateFormatter stringFromDate:empDate];        

        NSDateFormatter *currentDateFormatter = [[NSDateFormatter alloc]init];
        [currentDateFormatter setDateFormat:@"yyyy/M/d"];
        NSString *today = [currentDateFormatter stringFromDate:[NSDate date]];

        if([today isEqualToString:comapreDate]) {
            [returnArray addObject:dict];
        }
    }

    return ([returnArray count]) ? (NSArray*)returnArray : @[];
}

Upvotes: 0

Views: 89

Answers (4)

Nookaraju
Nookaraju

Reputation: 1668

try this , hope it help you.

NSArray *tempArray= [[self dataStore] valueForKeyPath:key_RootKey];
NSDateFormatter *empDateFormatter = [[NSDateFormatter alloc] init];
NSDate *referenceDate = [NSDate date];
[empDateFormatter setDateFormat:@"yyyy/M/d H:m"];
NSDate *todayDate = [empDateFormatter dateFromString:[empDateFormatter stringFromDate:referenceDate]];
[empDateFormatter setDateFormat:@"yyyy/M/d H:m"];

NSPredicate *findDates = [NSPredicate predicateWithBlock: ^BOOL(id obj, NSDictionary *bind){
    if (([todayDate compare:[empDateFormatter dateFromString:[obj valueForKey:key_StartDate]]]==NSOrderedSame)) {
        return YES;
    }
    return NO;
}];
NSArray *matchedRecords = [tempArray filteredArrayUsingPredicate:findDates];

Upvotes: 1

Shabib
Shabib

Reputation: 1697

Try this:

-(NSArray *)getCurrentShiftData
{
    NSMutableArray *returnArray = [NSMutableArray array];

    NSArray *tempArray = [self.dataStore valueForKeyPath:key_RootKey];

    NSDateFormatter *empDateFormatter = [[NSDateFormatter alloc]init];
    [empDateFormatter setDateFormat:@"yyyy/M/d H:m"];

    NSDateFormatter *currentDateFormatter = [[NSDateFormatter alloc]init];
    [currentDateFormatter setDateFormat:@"yyyy/M/d"];

    NSString *today = [currentDateFormatter stringFromDate:[NSDate date]];

    for(NSDictionary *dict in tempArray)
    {
        NSString *dt = [dict valueForKey:key_StartDate];
        NSDate *empDate = [empDateFormatter dateFromString:dt];

        [empDateFormatter setDateFormat:@"yyyy/M/d"];
        NSString *compareDate = [empDateFormatter stringFromDate:empDate];

        if([today isEqualToString:compareDate])
        {
            [returnArray addObject:dict];
        }
    }

    return (NSArray*)returnArray;
}
  1. You don't need to create the date formatters and todays date in each iteration. This is completely unnecessary.
  2. There is no need to check if the returnArray has values, it will return nil if it's empty.

Upvotes: 3

gaurish.salunke
gaurish.salunke

Reputation: 1079

Use NSPredicate

NSString *predicateStr = [NSString stringWithFormat:@"%@ == '%@'", key, value];
NSPredicate *predicate = [NSPredicate predicateWithFormat:predicateStr];
NSArray *filteredArray = [array filteredArrayUsingPredicate:predicate];

Upvotes: 0

Wain
Wain

Reputation: 119031

Don't worry about speed until you have a reason to and proof (from profiling) of where you performance issues come from.

That said:

  1. Don't create new date formatters on each iteration of the loop
  2. Create and store the dates once if you run this code repeatedly (then use a predicate)
  3. You don't need a condition in the return statement, either the array has some contents or it doesn't so creating a new empty array is pointless

Final point - profiling

Upvotes: 2

Related Questions