blonsky95 / AthleticsPointsCalculatoriOS

0 stars 0 forks source link

QUESTIONS FOR SWIFT MASTER / XCODE TRICKS #32

Open blonsky95 opened 3 years ago

blonsky95 commented 3 years ago
  1. if viewmodel is environmentobject, observableobject, and present in a struct. When I initialize the state variables, can I use the viewmodel? (I get the error that it hasn't been init, but should it be init if its a env object) - current solution, getting from a static function what I want. In the init block I can´t access the environmentObject, but I can access a Singleton/static instance. I can use the envObject at onAppear but this will be called everytime user navigates back, and it might be late(?).

  2. How would you do something like my WAPointsCalculator - DynamicPoints View in a clean way. At the moment parent struct, WAPC has a state variable for the selected group, it is binded to the DPV, which listens to changes and everytime there is a group change it resets the pickers, scores and everything. Within DPV, there is State array vars to bind them to their respective performance textfields, placementpoints textfield and pickerIndex. As these change (using onChange), the mainViewModel updates an eventGroupPointsHolder class with all this information. When user will press save, this is what will get saved, and turned into a core data entity.

  3. If you have a core data entity you use to save and load stuff, is there any limitation that you are using this class (created by core data) and not a normally created one, what if the class should inherit from another, can the coredata created class be a subclass of something I created? (Will try to create a core data entity for wa points performances, without its other class, although it doesnt need subclassing so not exactly the same). OK so the issue with this one lies in that the NSobject doesnt have the custom types, everything will be a String, so when I want to access a property I would have to decode it each time. So STACK OVERFLOW THIS - do I have a "cousin" class, that I convert to once and has my custom classes?, or use only the nsmanaged, and in every wrapped var decode the respective json to the custom class.

  4. When I create a core data entity, if a property is a custom codable type, if it contains a lot of information is it worth to save it to the entity? Or is it better to hold a key parameter and retrieve the full class later on? E.g. When I want to save a WAPointsPerformance I save to Core Data the full eventGroup which includes a few fields, an AthleticEvent, and an array of AthleticsEvents. Worth being more efficient or is this fine?

blonsky95 commented 3 years ago
  1. Published properties when debugging - you might encounter the problem where published properties dont display the information when you toggle the arrow. This is because you have _variable, which means that you are addressing the property wrapper. And the prop wrapper doesnt have that information to display. So, when in a breakpoint, go to command line, and you can do e variableName and this will print the information of the wrapped value, if you do e _variableName you would get info about the property wrapper.

  2. To set the initial value of a State variable - When you first start a view or struct there is a common problem where you want to initialize something. Options are, in the initialisation, but you can´t access other initilalised classes; in onAppear but this sucks because everytime user navigates back to the view it gets triggered so no bueno; or third option in the init block. But here you will get an error saying you are using something before intiialised. So, you can do the following to set the State variable to something _selectedEventGroup = State(initialValue: initialEventGroup) and with this, you can use a constructor parameter to set the initial value (this wouldn't be possible in the first option).