0

I have the following problem:

I have an application, which uses two tableViewControllers. The first TableViewController lists

struct Task{
   let type: String
   [other attributes]
}

These Tasks are stored in a TaskStore class, with a static shared instance which stores all Tasks in the application.

class TaskStore {
    var tasks: [Task] = []

   class var sharedInstance: TaskStore {
      struct Static {
          static let instance = TaskStore()
      }
      return Static.instance
   }
}

Now, for each Task, I need to store a set of Subtasks associated to that instance of Task. Hence, I created a Subtask struct

struct Subtask{
   let subtype: String
   [other attributes]
}

and created a SubtaskStore class, which I modified to no longer include the static instance, but instead:

class SubtaskStore {
   var subtasks: [Subtask] = []

   class var sharedInstance: SubtaskStore {
      let instance = SubtaskStore()
      return instance
   }
}

Note: This seems to me to be much more beneficial than simply including an array of Subtasks as a member of the Task struct, because I can create methods that operate of the SubtaskStore.sharedinstance.

Then I amended the Task struct as follows

struct Task{
   let type: String
   var subtasks: SubtaskStore // as oppose to: var subtasks [Subtask] = []
}

The idea is that now every Task has its own SubtaskStore, which stores all the Subtasks for the Task at hand.

First question: Is there a better way of modeling the hierarchy of Tasks to Subtasks? I am quite new to programming, but the design seemed intuitive.

I have successfully created an interactive TableViewController that displays the elements in TaskStore and updates the Table when a new Task is added/removed. I have now created a link between the Task TableViewController and the SubTask TableViewController; clicking on a TaskTableViewCell opens a new SubTaskTableViewController (currently empty).

Second question: Assume that the TaskTableViewCell selected by the user is in row 5. I want to display the elements in "TaskStore[4].subtasks" in the SubtaskTableViewController. How do I pass the number "4" from the selected TaskTableViewCell in the TaskTableViewController to the SubTaskTableViewController and display on the elements in the TaskStore[4].subtasks.sharedInstace?

user227837
  • 129
  • 1
  • 10

1 Answers1

0

I would argue that your architecture isn't a particularly good setup.

The big thing to note is that if you want to change some property of ALL tasks, like adding a color property, you need to implement it in two Structs, then update two separate view controllers to colorize. What happens if you want to allow subtasks to have subtasks - do we have to continuously create deeper structs of type SubSubTask? Even before we make any changes - I'm sure you wont be excited to implement two separate, yet almost identical, table view controllers.

The better method is to have a single struct, Task which allows for a subtasks array. Your TaskStore will only track the top-level tasks, subtasks are "owned" by their parent tasks.

struct Task {
    var children = [Task]()
    // other properties
}

class TaskStore {
    var tasks = [Task]()

   // Swift now supports static vars on classes
   static var sharedInstance = TaskStore()
}

With this setup, you can also just write a single table view controller which displays a list of tasks provided to it (from some source of your choice). That task list could be a sublist, but it would be irrelevant to the table view controller - which is great - because it makes your code simpler and much easier to maintain.


As for part two, you can pass information to other view controllers if you are using storyboards using the prepare for segue method of the sending view controller. There's lots of information on the topic here on SO.

How do you share data between view controllers and other objects in Swift?

Community
  • 1
  • 1
cpimhoff
  • 675
  • 6
  • 11