0

我正在显示活动列表的几个任务。为此,我使用 NSPredicate predicateWithFormat: 来选择列表中的项目。但我遇到的问题是关于该列表中先前删除的任务。即使它们没有显示(或者我应该说显示为无效),它们仍然被计算在内。

所以我的问题是:我怎样才能只选择仍然是列表一部分的项目(任务)?删除的数据应该被忽略。实际上应该完全删除它们。

我的部分代码(我刚刚根据马丁的评论更新了它):

- (void)continueAutomaticModeWithList:(DIDList *)list taskIndex:(NSInteger)index {
if (index == list.tasks.count) return;
DIDTask *task = [DIDTask MR_findFirstWithPredicate:[NSPredicate predicateWithFormat:@"list == %@ && index == %@", list, @(index)]];

if (task.completedValue) {
    [self continueAutomaticModeWithList:list taskIndex:index + 1];
    return;
}

[UIAlertView showAlertViewWithTitle:@"Task" message:task.name cancelButtonTitle:@"Stop" otherButtonTitles:@[@"Click to go to next action", @"Click to go to next action and notify user"] handler:^(UIAlertView *alertView, NSInteger buttonIndex) {
    if (buttonIndex > 0) {
        [MagicalRecord saveWithBlock:^(NSManagedObjectContext localContext) {
            [[task MR_inContext:localContext] setCompletedValue:buttonIndex == 1];
        } completion:^(BOOL success, NSError *error) {
            [self.tableView reloadData];
        }];
        [self continueAutomaticModeWithList:list taskIndex:index  1];
    }
}];

}

4

1 回答 1

1

如果我正确理解您的问题,则以下内容应该有效:

- (void)continueAutomaticModeWithList:(DIDList *)list taskIndex:(NSInteger)index {
    if (index == list.tasks.count) return;
    DIDTask *task = [DIDTask MR_findFirstWithPredicate:[NSPredicate predicateWithFormat:@"list == %@ && index >= %@", list, @(index)]
                                              sortedBy:@"index"
                                             ascending:YES];

    if (task == nil) {
         // No task with given or greater index found.
         return;
    }

    if (task.completedValue) {
        [self continueAutomaticModeWithList:list taskIndex:task.index + 1];
        return;
    }

    // ...
}

它不是搜索具有给定索引的对象(可能不再存在),而是搜索至少具有给定索引的“第一个”对象。

于 2013-11-10T20:43:58.233 回答