0
votes

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

}

1
Your problem is not clear to me. 1) What do you mean by "they are not displayed/displayed as void"? There is no code displaying anything in your question. - 2) How are tasks deleted, and what do you mean by "deleted tasks are still counted"? There is no "count" in your code. - 3) Is "index" an attribute of "DIDTask"? Where is it set? Do you assume that the "index" is updated when objects are deleted?Martin R
Martin, let's say I have a list name: "Start you computer" and the tasks related to it are 1) "Plug your computer", 2) "Plug all peripherals" and 3) "Press the start button". I don't have any problem if I want the app to select and display tasks 1), 2) and 3). But let's say I delete task 2), then it will display like this "Plug you computer", " ", "Press the start button". I should have just been "Plug your computer", "Press the start button". Task 2) is displayed as " " when it should not even be there.Armand
How do you display the tasks in the tableview? Do you use a fetched results controller? If not, how is the table view updated when a task is deleted? What does the fetch request look like?Martin R
They are displayed 1 by 1 in an alert view. See code above as I've just updated it.Armand
Deleting an object does not change the index attribute of the remaining objects. If you search for index == 1 and that object has been deleted, then you will get nil, not the next undeleted object.Martin R

1 Answers

1
votes

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.