Recursive Save not working

I’m trying to follow the sample code and I have a function like so

func testOrder() {


Types.tryblock({ () -> Void in
 
 // create the orders datastore
 var orders = Backendless.sharedInstance().persistenceService.of(Order().ofClass())
 
 var orderItem1 = OrderItem()
 orderItem1.name = "Printer"
 orderItem1.quantity = 1
 orderItem1.price = 99.0
 
 var orderItem2 = OrderItem()
 orderItem2.name = "Paper"
 orderItem2.quantity = 10
 orderItem2.price = 19.0
 
 var order = Order()
 order.orderNumber = 1
 order.orderName = "Office Supplies"
 order.orderItems.append(orderItem1)
 order.orderItems.append(orderItem2)
 
 let result = orders?.save(order) as? Order
 print("Order has been saved: \(result)")
 
 },
 catchblock: { (exception) -> Void in
 print("Server reported an error: \(exception as? Fault)")
 })
 }




I’m using the Cocoa Pod version of Backendless version 5.0.4
and when I log the result, it shows me:


Order has been saved: Optional(Order #0 'nil' [])

Can someone help me figure out what I’m doing wrong here?

Hello Jon,
Please check this documentation.

Assuming you want to create an Order object with 1:N relation to the OrderItem objects here are the correct steps:

@objcMembers
class OrderItem: NSObject {
    var objectId: String?
    var name: String?
    var quantity = 0
    var price = 0.0
}
@objcMembers
class Order: NSObject {
    var objectId: String?
    var orderNumber = 0
    var orderName: String?
    var orderItems: [OrderItem]?
}
func testOrder() {
        Types.tryblock({ () -> Void in
            
            // create the orderItems DataStore
            let orderItems = Backendless.sharedInstance().data.of(OrderItem.ofClass())
            
            // create and save first orderItem
            let orderItem1 = OrderItem()
            orderItem1.name = "Printer"
            orderItem1.quantity = 1
            orderItem1.price = 99.0
            let savedOrderItem1 = orderItems?.save(orderItem1) as! OrderItem
            
            // create and save second orderItem
            let orderItem2 = OrderItem()
            orderItem2.name = "Paper"
            orderItem2.quantity = 10
            orderItem2.price = 19.0
            let savedOrderItem2 = orderItems?.save(orderItem2) as! OrderItem
            
            // create the orders DataStore
            let orders = Backendless.sharedInstance().data.of(Order.ofClass())
            
            // create and save order
            let order = Order()
            order.orderNumber = 1
            order.orderName = "Office Supplies"
            let savedOrder = orders?.save(order) as! Order
            
            // add orderItems to the order
            let relationsSet = orders?.setRelation("orderItems:OrderItem:n", parentObjectId: savedOrder.objectId, childObjects: [savedOrderItem1.objectId!, savedOrderItem2.objectId!])
            print("Relations have been set")
        }, catchblock: { (exception) -> Void in
            print("Server reported an error: \(exception as? Fault)")
        })
    }

Regards, Olga

Thanks for the information. It’s unfortunate that I have to manage all of my relations myself.