0

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

}

Armand
  • 435
  • 1
  • 11
  • 23
  • 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 Nov 10 '13 at 19:07
  • 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 Nov 10 '13 at 19:41
  • 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 Nov 10 '13 at 19:46
  • They are displayed 1 by 1 in an alert view. See code above as I've just updated it. – Armand Nov 10 '13 at 20:30
  • 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 Nov 10 '13 at 20:34
  • Thanks Martin but how to fix this? How to get the next undeleted object? – Armand Nov 10 '13 at 20:37

1 Answers1

1

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.

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • Thanks it solves the issue. But another one rose: The last object is repeated. Anyway at least I see where to look now. – Armand Nov 10 '13 at 20:51
  • @Armand: You probably have to check if `MR_findFirstWithPredicate` returns `nil`, I have updated the answer. – Martin R Nov 10 '13 at 20:52
  • it still doubling last object adding your updating lines. But given the first problem has been solved, answer accepted! Now I'm working on fixing the second one... – Armand Nov 10 '13 at 21:07