Google Tasks Api iOS fetching with for loop

288 Views Asked by At

Im able to receive tasks but in each run the order of tasklists vary. I need to have the same order of task lists in each run.

How i fetch now is,

GTLServiceTasks *service = self.taskService;
query = [GTLQueryTasks queryForTasklistsList];
self.taskListsTicket = [service executeQuery:query
    completionHandler:^(GTLServiceTicket *ticket,
    id taskLists, NSError *error) {

    //i get the count of Tasklists here

    GTLTasksTaskList *itemtemp;
        for (int k=0; k<count_of_tasklists; k++) {
            NSMutableArray *alist = [[NSMutableArray alloc] init];

            //i get the id and titles of the tasklists here

            GTLServiceTasks *service2 = self.taskService;
            query2 = [GTLQueryTasks queryForTasksListWithTasklist:itemtemp.identifier];
            self.taskListsTicket = [service2 executeQuery:query2
                completionHandler:^(GTLServiceTicket *ticket2,
                id taskLists2, NSError *error2) {
                    self.taskLists = taskLists2;
                    for (int j=0; j<[self.taskLists.items count]; j++) {

                        //i get tasks for each tasklist here and store each one of
                        //the tasklists as a nsmutablearray

                    }
                    [tasks addObject:alist];

                    //i add each tasklist array to the master array
                    // then call reload method of the uitableview here

                }];
        }
}];

In the uitableview that i present the tasks i use section titles for tasklist titles and rows for task titles. In each run the correct tasks are presented under the correct sections.

My problem is that the order of tasklists (order of sections) are different in each run, both in my uitableview and console log of GTL replies. I think the for loop works without waiting the result from the Google Api call. Any ideas about the issue?

Thanx in advance

1

There are 1 best solutions below

0
On

executeQuery: performs its block asynchronously; there is no guarantee that the results are returned in the same order that the queries are issued.

Instead of invoking executeQuery: to get the tasks of each tasklist separately, those queries could be combined into a single batch. The batch query results will be in the same order as the batch queries.

It's also reasonable to just store the results in a dictionary, keyed by tasklist identifier, rather than in an array. The dictionary results can be iterated afterwards in a specific order, such as by using objectsForKeys:notFoundMarker: with an array of tasklist identifiers as the keys.