Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find UILocalNotification in scheduledLocalNotifications array without for loop?

Currently I am looping through all of my scheduled local notifications to find a "match" based upon a value in the userInfo dictionary object. This is seemingly very slow when I have 30+ local notifications set. Is there a way to access an individual local notification without traversing through the array?

Here is what I have:

NSArray *notificationArray = [[UIApplication sharedApplication]     scheduledLocalNotifications];
UILocalNotification *row = nil;
for (row in notificationArray) {
            NSDictionary *userInfo = row.userInfo;
            NSString *identifier = [userInfo valueForKey:@"movieTitle"];
            NSDate *currentAlarmDateTime = row.fireDate;
if([identifier isEqualToString:myLookUpName]) {
 NSLog(@"Found a match!");
}
}

Here is what I want:

NSArray *notificationArray = [[UIApplication sharedApplication]     scheduledLocalNotifications];
UILocalNotification *row = " The row in notificationArray where [userInfo valueForKey:@"movieTitle"]=myLookUpName" ;
like image 200
Eric Avatar asked Feb 21 '23 19:02

Eric


1 Answers

You might be able to use predicates for this, but I have not tested it:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"userInfo.movieTitle = %@", myLookUpName];

And then use that predicate to filter the array and grab the first element:

UILocalNotification *row = [[notificationArray filteredArrayUsingPredicate:predicate]objectAtIndex:0];

Again, this is untested and may not work.

EDIT

If that doesn't work, you can use a test block:

UILocalNotification *row = [[notificationArray objectsAtIndexes:[notificationArray indexesOfObjectsPassingTest:^(id obj, NSUInteger idx, BOOL *stop){
    return [[[obj userInfo]valueForKey:@"movieTitle"] isEqualToString:myLookUpName];
}]]objectAtIndex:0];
like image 71
Evan Mulawski Avatar answered May 16 '23 07:05

Evan Mulawski