last day (15 days later) » 

1:04 PM
0
Q: Storing a sub entities id in a parent entities id array?

infernoukI have many user created exercises stored in an entity, I want to save specific ones into 1 parent 'routine'. Each exercise has a UUID generated upon saving, my logic is that I can store these UUID's in a routine entity to group them up. Hence in future I can recall the routine and have the u...

 
You should consider using a to-many relationship from Routine to Exercise, instead of having an array of Exercise UUIDs as an attribute of the Routine entity. Using a relationship avoids the need for UUIDs and will make it easier to set and get the Exercises for a given Routine (and vice versa).
 
oh really? how will i be able to determine which exercises belong to which routine when there are multiple routines all with their own exercises?
 
You add Exercise objects themselves to the relationship, not their UUIDs. Behind the scenes, CoreData manages the unique IDs for you.
 
ah ok, do you have any guides that may be of use to this so I can resolve how to implement it?
 
Take a look at this video from Ray Wenderlich. He models Devices and Persons, where each person can have many devices, whereas you have Routines and Exercises, and each routine can have many exercises.
 
1:04 PM
looking at this tutorial, should I be constructing the routine entity before the exercises, then allowing the user to add exercises to that existing routine entity? Rather than currently where I try to form a routine entity when the save button is pressed on a list of exercises?
 
You can do it either way - whichever makes most sense in terms of your application design. Your way, each new exercise will initially not be related to any Routine, but when save is tapped, you create a Routine and assign them all to that.
 
yes thats the plan, however I cant see how that works as you cant set the theoretical 'userExercise.userRoutine.name = routinesName' property before this point to be able to link the exercise to a specific routine? You would have to create the routine first so that this property is accessible? Or is there another way?
 
Sounds like it's easiest to create the routine first. But you could instead just leave userExercise.userRoutine = nil, and when save is tapped, create the routine, set newRoutine.name = routinesName, and then iterate through your array of Exercises setting eachExercise.userRoutine = newRoutine.
 
So is it possible to do it as I am currently, but then when save is tapped for the routine to create its entity, it writes userExercise.userRoutine.name = routinesName (left as nil when creating the exercise initially) and it'll apply that to all the exercises that were constructed? How can I tell the app to apply it to every exercise row in the 'workout designer' table though and not just every exercise in the database? I cant think of how that selection of specific exercises would work... Cheers!
 
Presumably you have an array of Exercises that is used to populate the tableView. You could just iterate through that array.
 
1:17 PM
//
// WorkoutCreatorView.swift
// My App
//
// Created by Jack Warris on 07/11/2016.
// Copyright © 2016 com.black-integratedcreative. All rights reserved.
//

import Foundation
import UIKit
import CoreData

class WorkoutDesignerController: UIViewController, UITableViewDataSource, UITableViewDelegate, NSFetchedResultsControllerDelegate {

// MARK: - DECLARATIONS

private let persistentContainer = NSPersistentContainer(name: "Lift")
private let segueAddNewUserExerciseViewController = "addNewExerciseSegue"
just thought id send the full view controller so its more easy to see in context
 
1:28 PM
Thanks. So you are using a fetchedResultsController to populate the table view. If you amend the fetch underlying the FRC to include only those Exercises where userRoutine is nil ("fetch.predicate = NSPredicate(format:"userRoutine == nil")"), the TV will only display newly created Exercises, not those which already belong to a Routine. Then in your createRoutine method, you can iterate through the fetchedObjects and set the relationship to the newly created userRoutine.
 
1:42 PM
thanks i get it, ill give that a go and let you know here if i have issues
 
 
1 hour later…
2:49 PM
im using userExercise.usersroutine?.name = nil to set the userExercise to have a nil name property. However when i try adn save an exercise the app has a fatal error. Am i approaching saving this wrong? the 'usersroutine' is the identifier of the relationship that links to the routine.
 
 
6 hours later…
9:02 PM
@infernouk Sorry, had to run. Any luck?
 
 
1 hour later…
10:17 PM
hey, i couldnt get my head around the fetching of exercises with a nil routine, I made another post about it as it was more specific a query, it can be found here if you want to reply otherwise here is fine! stackoverflow.com/questions/40891946/…
 

last day (15 days later) »