gpt4 book ai didi

ios - 应用程序在完成处理程序中崩溃并出现 EXC_BAD_ACCESS 错误

转载 作者:行者123 更新时间:2023-11-30 11:50:12 24 4
gpt4 key购买 nike

任何人都可以帮我修复我的 API 调用功能吗?我解析数据并将其存储在结果中,如果解析时出现任何错误,我会抛出失败 json 错误。如果解析成功,那么我将数据保存在数据库中。但是在 json 解析失败后我的应用程序在这一行崩溃了

 return completion(.failure(.jsonError("Values of expenses not found")))

这是我的整个功能:-

func loadExpenseOfReport(organizationID: Int, reportID: Int, completion: @escaping (Result) -> Void) {

self.remote.loadExpensesOfReport(organizationID, reportID: reportID) { result in
switch result {

case .success(let json):
var result : (json: [[String: Any]], ids: [Int])
do {
result = try Expense.processExpenses(JSON: json!, organizationID: Int(self.user.organization.organizationID))
} catch let error {
return completion(.failure(.jsonError("Values of expenses not found"))) // App crashes here
}
print("result \(result)")
// Save in database
self.dataStack.enqueueCoreDataBlock({ context in
Sync.changes(result.json, inEntityNamed: "Expense", predicate: nil, parent: nil, parentRelationship: nil, inContext: context, operations: .all, completion: { error in
guard error == nil else {
return completion(.failure(.syncError(error!)))
}
return completion(.success(nil))
})
})
//return completion(.success(nil))

case .failure(let error):
return completion(.failure(error))
}
}

但是当我将上面保存在数据库中的行更改为此时,它不再崩溃。

//self.dataStack.enqueueCoreDataBlock({ context in
Sync.changes(result.json, inEntityNamed: "Expense", predicate: nil, parent: nil, parentRelationship: nil, inContext: self.dataStack.viewContext, operations: .all, completion: { error in
guard error == nil else {
return completion(.failure(.syncError(error!)))
}
return completion(.success(nil))
})
//})

最佳答案

您不需要“返回”完成 block 。就像这样调用它:

var result : (json: [[String: Any]], ids: [Int])
do {
result = try Expense.processExpenses(JSON: json!, organizationID: Int(self.user.organization.organizationID))
} catch let error {
completion(.failure(.jsonError("Values of expenses not found")))
}

关于ios - 应用程序在完成处理程序中崩溃并出现 EXC_BAD_ACCESS 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48406732/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com