For Core from Core Data

时间:2015-03-31 06:34:18

标签: core-data for-loop uilocalnotification nsfetchrequest

我有一个问题,我有2个数组(日期和描述),一个是保留我从datePicker中选择的日期,另一个是带字符串的数组,两个数组都是从CoreData获取的。

-(void)generateLocalNotification {
    CoreDataStack *coreDataStack = [CoreDataStack defaultStack];
    NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"AddEntrySettings"];
    fetchRequest.resultType = NSDictionaryResultType;
    NSArray *result = [coreDataStack.managedObjectContext executeFetchRequest:fetchRequest error:nil];
    NSMutableArray *date = [result valueForKey:@"date"];
    NSMutableArray *descriere = [result valueForKey:@"descriere"];`

    if (date != nil) {
        for (NSString *stringDate in date) {
            NSDateFormatter *format = [[NSDateFormatter alloc]init];
            [format setDateFormat:@"MM/dd/yyyy h:mm a"];
            [format setTimeZone:[NSTimeZone  timeZoneWithName:@"GMT"]];
            self.date = [format dateFromString:stringDate];
            NSLog(@"LOG:%@",date);
            localNotification.fireDate = [self.date dateByAddingTimeInterval:0];
            localNotification.timeZone = [NSTimeZone timeZoneWithName:@"GMT"];

            for (int i = 0; i < descriere.count; i++) {
                localNotification.alertBody = descriere[i];
            }
            localNotification.applicationIconBadgeNumber = 1;
            localNotification.soundName = UILocalNotificationDefaultSoundName;
            localNotification.userInfo = @{@"id" : @42};

            UIApplication *app = [UIApplication sharedApplication];
            [app scheduleLocalNotification:localNotification];
        }
    }
}

当我尝试fireDate时,一切正常,每当数组中的日期与本地时间匹配时,我会收到通知,直到我尝试添加alertBody,当我为alertBody创建for循环时,每次都显示最后一次从我的NSArray进入。在CoreData中,我同时添加了两个条目。我的错误在哪里?如何使用alertBody接收与我在CoreData中插入的日期相匹配的通知?

1 个答案:

答案 0 :(得分:0)

问题在于这个for循环:

       for (int i = 0; i < descriere.count; i++) {
            localNotification.alertBody = descriere[i];
        }
对于每个stringDate

将迭代到descriere数组中的最后一项。您想要的是在stringDate中找到date的索引,然后在descriere中找到相同索引处的字符串。

但有一种更简单的方法。不要将result解压缩到两个独立的数组中,只需从for循环中访问不同的值:

if (result != nil) {
    for (NSDictionary *dict in result) {
        NSString *stringDate = [dict objectForKey:@"date"];
        // if necessary, test whether stringDate is nil here
        NSDateFormatter *format = [[NSDateFormatter alloc]init];
        [format setDateFormat:@"MM/dd/yyyy h:mm a"];
        [format setTimeZone:[NSTimeZone  timeZoneWithName:@"GMT"]];
        self.date = [format dateFromString:stringDate];
        NSLog(@"LOG:%@",date);
        localNotification.fireDate = [self.date dateByAddingTimeInterval:0];
        localNotification.timeZone = [NSTimeZone timeZoneWithName:@"GMT"];

        localNotification.alertBody = [dict objectForKey:@"descriere"];          
        localNotification.applicationIconBadgeNumber = 1;
        localNotification.soundName = UILocalNotificationDefaultSoundName;
        localNotification.userInfo = @{@"id" : @42};

        UIApplication *app = [UIApplication sharedApplication];
        [app scheduleLocalNotification:localNotification];
    }
}