2012-04-15 2 views
4

現在、私はスケジュールされたすべてのローカル通知をループして、userInfoディクショナリオブジェクトの値に基づいて「一致」を検出しています。 30以上のローカル通知が設定されていると、これは非常に遅いようです。配列をたどることなく個々のローカル通知にアクセスする方法はありますか?ここでforループなしでscheduledLocalNotifications配列のUILocalNotificationを検索しますか?

は私が持っているものです:あなたはこのために述語を使用することができるかもしれません

NSArray *notificationArray = [[UIApplication sharedApplication]  scheduledLocalNotifications]; 
UILocalNotification *row = " The row in notificationArray where [userInfo valueForKey:@"movieTitle"]=myLookUpName" ; 

答えて

8

が、私はそれをテストしていない:ここで

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!"); 
} 
} 

は、私が欲しいものである

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

この述語を使用して配列をフィルタリングし、最初の要素を取得します。

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

また、これはテストされておらず、動作しない可能性があります。

EDIT

問題が解決しない場合は、テストブロックを使用することができます。

UILocalNotification *row = [[notificationArray objectsAtIndexes:[notificationArray indexesOfObjectsPassingTest:^(id obj, NSUInteger idx, BOOL *stop){ 
    return [[[obj userInfo]valueForKey:@"movieTitle"] isEqualToString:myLookUpName]; 
}]]objectAtIndex:0]; 
+0

あなたはテストブロックコードで何が起こっているのか私に説明できますか?私は "コピーして、私のコードに貼り付けようとしましたが、エラーが発生します: 'BOOL(^)(ID、NSUInteger、BOOL * NSUInteger、BOOL *) ' – Eric

+1

@エリック:なぜあなたはそのエラーを受けているのか分かりません。 http://www.fieryrobot.com/blog/2010/06/20/being-a-blockhead/で問題を特定することができます。 '' ' UILocalNotification *行= [[notificationArray objectsAtIndexes:[notificationArray indexesOfObjectsPassingTest:^ –

+0

は後半、ここでリアルタイムで来るが、この偉大な答えを越えつまずくが、コードのタイプミスによってスローされた他の誰のために、これを試してください(id obj、NSUInteger idx、BOOL * stop){return [[[obj userInfo] valueForKey:@ "movieTitle"] isEqualToString:myLookUpName]; }]] objectAtIndex:0]; '' ' –