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))
            }
        }
    }

1 Answer 1

1

You can create and afterDelete trigger to delete the children:

Parse.Cloud.afterDelete('Invoice', ({ object }) => {
  return Parse.Object.destroyAll(object.get('arrayField'));
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.