问题描述
我有一些代码应该检查是否存在某些 ID 并通过在多个表中添加新行来更新数据库。但是我无法让它工作,我的代码绝对不是最佳的。
能否请您帮我让它工作并向我展示执行此操作的正确方法?
这是我用来检查和更新数据库的类:
class UpdatePlanDB{
private var UserPlanCheckIsGood : Bool = false
private var PlanCheckIsGood : Bool = false
private var bothChecked : Int = 0
func getNewPlanId(completionHandler: @escaping (Result<Int,Error>) -> Void){
getLowestPlanId(){ result in
var lowestPlanId : Int = 2
switch(result){
case .success(let res):
lowestPlanId = res
case .failure(let err):
completionHandler(.failure(err))
}
if(lowestPlanId >= 2){
completionHandler(.success(lowestPlanId - 1))
}else{
var highestPlanId : Int = 0
self.getHeighestPlanId(){ resultH in
switch(resultH){
case .success(let res):
highestPlanId = res
case .failure(let error):
completionHandler(.failure(error))
}
completionHandler(.success(highestPlanId + 1))
}
}
}
}
func getLowestPlanId(completionHandler: @escaping (Result<Int,Error>) -> Void){
let request = NSMutableuRLRequest(url: NSURL(string: URL_GET_GET_LOWEST_PLAN_ID)! as URL)
request.httpMethod = "POST"
//request.httpBody = postString.data(using: String.Encoding.utf8)
let task = URLSession.shared.dataTask(with: request as URLRequest) { data,response,error in
if error != nil{
print(error!)
completionHandler(.failure(NetworkError.badURL))
return
}else if let data = data {
if var jsonString = String(data: data,encoding: .utf8) {
jsonString = jsonString.trimmingCharacters(in: CharacterSet(charactersIn: "0123456789.").inverted)
completionHandler(.success(Int(jsonString)!))
}
}
}
task.resume()
}
func getHeighestPlanId(completionHandler: @escaping (Result<Int,Error>) -> Void){
let request = NSMutableuRLRequest(url: NSURL(string: URL_GET_GET_HIGHEST_PLAN_ID)! as URL)
request.httpMethod = "POST"
//request.httpBody = postString.data(using: String.Encoding.utf8)
let task = URLSession.shared.dataTask(with: request as URLRequest) { data,encoding: .utf8) {
jsonString = jsonString.trimmingCharacters(in: CharacterSet(charactersIn: "0123456789.").inverted)
completionHandler(.success(Int(jsonString)!))
}
}
}
task.resume()
}
func checkIdDoesNotExist(planId: Int,tableName: String,completionHandler: @escaping (Result<Bool,Error>) -> Void){
let request = NSMutableuRLRequest(url: NSURL(string: URL_CHECK_PLAN_ID_EXISTS)! as URL)
request.httpMethod = "POST"
let postString = "planId=\(planId)&tableName=\(tableName)"
request.httpBody = postString.data(using: String.Encoding.utf8)
let task = URLSession.shared.dataTask(with: request as URLRequest) { data,error in
if error != nil{
print(error!)
completionHandler(.failure(NetworkError.badURL))
return
}else if let data = data {
if let jsonString = String(data: data,encoding: .utf8) {
if(jsonString == "0 results"){
completionHandler(.success(true))
}else{
completionHandler(.success(false))
}
}
}
}
task.resume()
}
func addToUserPlans(planId: Int,Error>) -> Void){
checkIdDoesNotExist(planId: planId,tableName: "UserPlans"){ result in
switch(result){
case .success(let res):
self.UserPlanCheckIsGood = res
self.bothChecked += 1
case .failure(let err):
print(err)
self.UserPlanCheckIsGood = false
self.bothChecked += 1
}
}
checkIdDoesNotExist(planId: planId,tableName: "Plans"){ result in
switch(result){
case .success(let res):
self.PlanCheckIsGood = res
self.bothChecked += 1
case .failure(let err):
print(err)
self.PlanCheckIsGood = false
self.bothChecked += 1
}
}
while(bothChecked != 2){
print("waiting")
}
if(PlanCheckIsGood && UserPlanCheckIsGood){
let request = NSMutableuRLRequest(url: NSURL(string: URL_ADD_USER_PLANS)! as URL)
request.httpMethod = "POST"
let postString = "userId=\(Auth.auth().currentUser?.uid ?? "None")&planId=\(planId)"
request.httpBody = postString.data(using: String.Encoding.utf8)
let task = URLSession.shared.dataTask(with: request as URLRequest) { data,error in
if error != nil{
print(error!)
completionHandler(.failure(NetworkError.badURL))
return
}else if let data = data {
if let jsonString = String(data: data,encoding: .utf8) {
if(jsonString == "true"){
completionHandler(.success(true))
}else{
completionHandler(.success(false))
}
}
}
}
task.resume()
}else{
completionHandler(.failure(NetworkError.badURL))
}
}
func addplan(planId: Int,Error>) -> Void){
if(PlanCheckIsGood && UserPlanCheckIsGood){
let request = NSMutableuRLRequest(url: NSURL(string: URL_ADD_PLAN)! as URL)
request.httpMethod = "POST"
let postString = "userId=\(Auth.auth().currentUser?.uid ?? "None")&planId=\(planId)"
request.httpBody = postString.data(using: String.Encoding.utf8)
let task = URLSession.shared.dataTask(with: request as URLRequest) { data,encoding: .utf8) {
if(jsonString == "true"){
completionHandler(.success(true))
}else{
completionHandler(.success(false))
}
}
}
}
task.resume()
}else{
completionHandler(.failure(NetworkError.badURL))
}
}
func addProgressRow(completionHandler: @escaping (Result<Int,Error>) -> Void){
}
}
我是这样称呼它的:
let newUpdate = UpdatePlanDB()
newUpdate.getNewPlanId(){result in
switch(result){
case .success(let res):
newUpdate.addplan(planId: res){ result2 in
switch(result2){
case .success(let res2):
print(String(describing: res2))
case .failure(let err2):
print(err2)
}
}
newUpdate.addToUserPlans(planId: res){ result2 in
switch(result2){
case .success(let res2):
print(String(describing: res2))
case .failure(let err2):
print(err2)
}
}
case .failure(let err):
print(err,"---------------------------")
}
}
以下函数工作正常:getNewPlanId()、getLowestPlanId()、getHeighestPlanId() 和 checkIdDoesNotExist()。
在代码运行的地方,如果成功,它应该返回“true”两次。 如有任何问题,请随时提出。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)