Back4App - 指向未保存对象的指针 代码:106,版本:1.19.1

问题描述

我有一个结构:

  1. Invoice 类。
  2. Invoice 包含 ArrayInvoiceEmployee 指针
  3. InvoiceEmployee 包含 InvoiceItem 个对象的数组
for employeeDataSet in someDataSet { 

for viewmodelData in someDataSet { 
let pfInvoiceItem = PFObject(className: "InvoiceItem")
pfInvoiceItem.setValue(viewmodelData.customerRate,forKey: "customerRate")
pfInvoiceItem.setValue(viewmodelData.customerHours,forKey: "customerHours")
pfInvoiceItem.setValue(viewmodelData.total,forKey: "total")
invoiceItems.append(pfInvoiceItem)
}


let pfInvoiceEmployee = PFObject(className: "InvoiceEmployee")
pfInvoiceEmployee.setValue(employeeDataSet.username,forKey: "username")
pfInvoiceEmployee.setValue(employeeDataSet.projectEmployee,forKey: "projectEmployee")
pfInvoiceEmployee.setValue(invoiceItems,forKey: "invoiceItems")

我收到此错误

Pointer to an unsaved object. (Code: 106,Version: 1.19.1)

关于这个保存操作:

func createInvoice(with invoice: Invoice,completion: @escaping (Result<Bool,NSError>) -> ()) {
        
        guard let payPeriod = invoice.payPeriod?.getPFPayPeriod(),let project = invoice.project?.getPFProject() else {
            let error = NSError(domain: "OURTEAM",code: 0,userInfo: [NSLocalizedDescriptionKey: "Save invoice failure"])
            completion(.failure(error))
            return
        }
        
        
        let parSEObject = PFObject(className:"Invoice")
        parSEObject["project"] = project
        parSEObject["payPeriod"] = payPeriod
        parSEObject["title"] = "Some"
        parSEObject["total"] = invoice.total
        parSEObject["invoiceEmployees"] = invoice.getPFInvoiceEmployees()

        parSEObject.saveInBackground {
          (success: Bool,error: Error?) in
          if (success) {
            completion(.success(true))
          } else {
            let error = NSError(domain: "OURTEAM",userInfo: [NSLocalizedDescriptionKey: "Create invoice failure"])
            completion(.failure(error))
          }
        }
    }

但是当我查看管理面板时,我可以看到所有记录都已正确保存。这意味着 InvoiceItem 记录获得了所有 objectIds 并且需要的列设置了值。相同的 fo InvoiceEmployee 记录和数据库中的 Invoice 记录。

还有 parSEObject.saveInBackground 返回成功。

要知道在我保存 InvoiceItem 之前我没有保存 InvoiceEmployeeInvoice

通常我使用联合类,没有这样的问题,因为我可以单独保存对象。现在我想按需保存发票(当用户完成所有本地记录时)。

我想可能是首先创建 Invoice 然后将所有需要的数据添加到其中的解决方案。但这意味着我需要等待完成所有与将员工及其项目添加Invoice 相关的回调,如果用户取消发票的创建,那么我需要删除数据库中的记录,这是一个有点过载的流程。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)