Reputation: 5667
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
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
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;
}
Upvotes: 3
Reputation: 1079
Use NSPredicate
NSString *predicateStr = [NSString stringWithFormat:@"%@ == '%@'", key, value];
NSPredicate *predicate = [NSPredicate predicateWithFormat:predicateStr];
NSArray *filteredArray = [array filteredArrayUsingPredicate:predicate];
Upvotes: 0
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:
Final point - profiling
Upvotes: 2