0

I have an Custom class Invoice which contains an Array of custom classes InvoiceItems.

When I remove Invoice I would like to remove all records associated with it. Currently when I removed Invoice the invoice items are still listed in database.

I supposed when you remove parent records it should remove nested items as I am using Array not Relation or Pointer.

func removeInvoice(with invoice: Invoice, completion: @escaping (Result<Bool, NSError>) -> ()) {
        
        guard let invoiceObjectId = invoice.getPFInvoice()?.objectId else {
            let error = NSError(domain: "OURTEAM", code: 0, userInfo: [NSLocalizedDescriptionKey: "Remove invoice failure"])
            completion(.failure(error))
            return
        }
        
        let query = PFQuery(className: "Invoice")
        query.whereKey("objectId", equalTo: invoiceObjectId)
        
        query.findObjectsInBackground { (objects, error) in
            
            if let unwrappedError = error as NSError? {
                completion(.failure(unwrappedError))
            }
            
            if let unwrappedObjects = objects {
                
                for object in unwrappedObjects {
                    object.deleteEventually()
                }
                
                completion(.success(true))
            }
        }
    }
Matrosov Oleksandr
  • 25,505
  • 44
  • 151
  • 277

1 Answers1

1

You can create and afterDelete trigger to delete the children:

Parse.Cloud.afterDelete('Invoice', ({ object }) => {
  return Parse.Object.destroyAll(object.get('arrayField'));
});
Davi Macêdo
  • 2,954
  • 1
  • 8
  • 11