0

I have an entity Exams, with a one-to-many relationship with another entity, Topics.

When the user clicks an exam on a table view, they go through to a page showing the topics associated with that exam. The topics page table view is in two sections, “To do” and “Completed”. Topics are organised into section by a Boolean attribute topicIsCompleted. As expected, when the user clicks the checkbox on the row, it toggles the Boolean and therefore the moves the topic into the other section.

What I want to be able to do is establish the number of topics complete and the number incomplete.

I can’t count the number of rows in each sections because if there are only incomplete topics or only complete topics, there will only be one section in the table view and I won’t know what type of topic (complete or incomplete) it contains.

I think I need to count the number of topics with a topicIsCompleted Boolean value of false and true….but this is what I can’t work out how to do.

Doing a loop of the topics associated with the particular exam, and using valueForKey topicIsCompleted doesn’t seem to work with a Boolean.

I’m using NSFetchedResultsController

My plan B is to create another fetch request with a predicate to only give me topics with topicIsCompleted == TRUE and count that.

Will there be issues creating a second fetch request?

Can I make it so my table view always draws two sections but one of them may be empty? I don’t mind there being sectionHeader in the view with no rows under it. That way I know the first section relates to incomplete and the second section relates to complete.

Are there any suggestions for an alternate solution?

Many thanks

Lukenn77
  • 19
  • 1
  • 3
  • Can’t you get this information from the NSFetchedResultsController? – Joakim Danielson Jun 21 '22 at 20:51
  • "I can’t count the number of rows in each sections because if there are only incomplete topics or only complete topics, there will only be one section in the table view and I won’t know what type of topic (complete or incomplete) it contains." This sounds like a flaw of your data model, and that it's too coupled to your table. Conceptually, your parent object "student", or whatever always has two categories: todo, and completed. One of them just might happen to be empty, and you might not want to render a section header for it, but that's a concern of the view, not your data model. – Alexander Jun 21 '22 at 20:52
  • @Alexander, fair point, but my numberOfSections is dictated by fetchedResultsController.sections.count. – Lukenn77 Jun 22 '22 at 08:36
  • @JoakimDanielson - I'm sure I can I just don't know how. In the end I've created two Int attributes in the Exam entity and they keep track of the number of topics associated with the exam and the number of topics marked as complete. Probably a bloated way of doing it but that what I've worked out how to do – Lukenn77 Jun 22 '22 at 08:39
  • @Lukenn77 don’t do that, it’s a bad idea. Not because it’s bloated (unless you have millions of records, a few extra ints here and there are irrelevant), but because it’s semantically incorrect. This is called denormalization, which is store a copy of information in a way that’s more convenient to access. The goal is to usually improve performance, e.g. suppose you had a web api for books. Your dab might model a library as a table that has the ids of the books. However, querying the web api for the book names every time you need them is too expensive and slow. Instead, you might save a copy … – Alexander Jun 22 '22 at 16:45
  • … in that case, storing a copy of the name, author, etc. might make sense. But denormalization always comes at a cost: first, you use extra storage. This is almost always irrelevant. But worse, you introduce an opportunity of serious bugs. You now have a split source of truth: two sources for the same data, which should always be the same, but which can now differ. What happens if your exam says it has 3 todos, but there are 4 associated todo objects? Is it the count that’s wrong and should be updated to 4, or should one of the todos be ignored? Which one? It’s a bad design. – Alexander Jun 22 '22 at 16:47
  • 1
    Instead, you can look at the objects in your `sections` array. Each one conforms to the [`NSFetchResultsSectionInfo`](https://developer.apple.com/documentation/coredata/nsfetchedresultssectioninfo) protocol, which has an `objects` property. You can look at those objects (after casting them as necessary) to check if they’re complete or incomplete. – Alexander Jun 22 '22 at 16:49
  • Thank you @Alexander, I've been able to change how I do the calculation using your advice and can scrap the extra attributes (I agree, is not a good way to do it!) – Lukenn77 Jun 23 '22 at 19:17

0 Answers0