Class with relational array (Swift)

I have a Class with an array of another class, much like your Order and OrderItem, but it doesn’t create the relational table for some strange reason. The FDWeek is set up in a viewcontroller and seven FDDays are created and added before the FDWeek get saved.









 @IBAction func saveBtnTapped(sender: UIBarButtonItem) {

        debugPrint("saveBtnTapped")

        

        

        if newRecord {

            debugPrint("create new week")

            self.dismissKeyboard()

            let newWeek = FDWeek()

            newWeek.startDate = Utils.dateFromString(startDateTF.text!, format: "dd/MM/yyyy")

            newWeek.weight    = (weightTF.text! != "") ? Double(weightTF.text!)! : 0.0

            newWeek.pTarget   = (pGramTF.text! != "") ? Double(pGramTF.text!)! : 0.0

            newWeek.fTarget   = (fGramTF.text! != "") ? Double(fGramTF.text!)! : 0.0

            newWeek.cTarget   = (cGramTF.text! != "") ? Double(cGramTF.text!)! : 0.0

            newWeek.suppPlan  = suppPlanTF.text

            

            for var index = 0; index < 7; index++ {

                let date      = Utils.addDaysToDate(newWeek.startDate, daysToAdd: Double(index))

                let day       = FDDay()

                day.date      = date

                newWeek.days.append(day)

                debugPrint("Day: \(Utils.stringFromDate(date, format: "dd/MM/yyyy"))")

            }

            debugPrint(newWeek.days)

            self.dismissViewControllerAnimated(true, completion: nil)

            newWeek.saveToServer()

        } else {

            debugPrint("update week")

        }

    }

I have attached the Classes as a .zip file. I can see the FDWeek class in Backendless, but the FDDay doesn’t get created. What am I doing wrong?

Archive.zip (2.3kB)

Hi Jorgen,

Make sure FDDays extends from NSObject. That should take care of it.

Regards,
Mark

Thanks Mark. Was getting blind looking for a solution.

I also have a Meal that is an array of Day, and a Portion that is an array of Meal.
If I carry on the same as for Day, do I get all of them when I fetch the Week?

I seem to have a problem with saving the other classes as the arrays are empty. They would only be filled as the user get deeper. To overcome this problem I could put a dummy instance into the array, and later get rid of it, or is there a smarter way around it?

error message : Cannot save entity with primitive collection property

To avoid this problem, make sure to declare the schema on the server. When backend receives an object with empty arrays, it does not know what type would be stored in it. However, if you declare all the relation columns in your tables, you’d be able to avoid this problem.

So, if I run it once setting up dummy objects in the arrays so Backendless creates the tables. Afterwards I don’t have to do this and the user can add stuff to the arrays at a later stage?

The problem appears only when the schema is not defined on the server. Once you create the table and declare all the relationships, saving objects with empty arrays will work.

Thanks Mark!