lundi 7 septembre 2020

Swift 5 Firestore Network Layer architecture - What is the optimal practice to handle Firestore updates to models?

Let's say I have an app to track daily workouts. Call the landing page LogVC, which uses a DayViewModel to present data.

DayViewModel {
  var date: String
  var exercise: [AnyWorkout]
  
  private var service = DayService()
}

The array of AnyWorkout is a protocol to allow users to either add a pre-made training routine (Routine) or a single exercise, (Exercise) to their daily log. Each object has their own ViewModels and DetailVCs for CRUD operations.

I'm using Firestore for storage. Ideally, I'd like to break apart my network layer to handle specific portions of data (DayService for updates/edits to Day, RoutineService for updates/edits to Routine, etc). The idea behind it is that updating an Exercise may not require updating a Routine or Day, and updating a Routine may not require updating an Exercise or Day.

As an example, a user can create a new Exercise to store in their list of [Exercise], which would have nothing to do with the current day.

HOWEVER, if the user chooses to add an Exercise to their log in the current Day, they WOULD need to update the current DAY. But as of right now, that option takes place in a childVC, either ExcericeListVC or ExerciseDetailVC (user taps addButton in LogVC, which presents ExerciseListVC. User can add from here or tap into Exercise to see detail and add from there)

ExerciseDetailVC: ViewController {
  
  var exerciseVM: ExerciseViewModel
  // init sets viewModel

  func addExerciseToCurrentDay() {
    ????
  }
}

Currently, the ExerciseViewModel has its own service to update itself, but not to update a Day.

Are there too many places to perform the update? I feel like most apps give users a variety options and locations to update items.

Is it better to simply combine the different services into one NetworkService in order to handle this?

Or should I pass the DayViewModel into the other VCs (ExerciseDetailVC in this example, or RoutineDetailVC if adding a Routine to the current Day) in order to call the update?

Or is the structure of the app completely screwed up that I have to redesign?

Typically I've always used a Singleton to handle all of my network needs (FirestoreManager), but I'd like to try an actual pattern instead. I'm still doing research on available patters (there seems to be so many!), but I figured I'd reach out here to get some advice.

Aucun commentaire:

Enregistrer un commentaire