Pregunta

I am displaying several tasks of a list of activities. To achieve this I am using NSPredicate predicateWithFormat: to select the items of the list. But the problem I have is regarding previously deleted tasks of that list. Even though they are not displayed (or I should say displayed as void), they are still counted.

So my question is: How can I only select items (tasks) that are still part of the list? Deleted data should be left out. They should completely be removed actually.

Part of my code (I have just updated it following Martin's comment):

- (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];
    }
}];

}

¿Fue útil?

Solución

If I understand you problem correctly, the following should work:

- (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;
    }

    // ...
}

Instead of searching for an object with the given index (which might not exist anymore), it searches for the "first" object which has at least the given index.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top