firebase:每次我循环通过数组时运行交易



我正在尝试通过数组循环并运行事务以将每个项目上传到firebase上,但不确定我想做的是可能的。

这个想法是:我有一系列问题["problemType1", "problemType2", ... "problemType10"],我给用户n时间来解决它。最后,我将解决的问题放在阵列中,然后将它们上传到Firebase上。如果问题存在于DB中,只是为了更新其价值。

这样,我想跟踪玩家使用哪种类型的问题来解决。目前,我写的代码仅上传一个问题。我在做什么错?

func uploadTheResolvedProblemsToDB(problems: [String], uid: String) {
let refDB = FIRDatabase.database().reference().child("users").child(uid).child("problems")
for problem in problems {
    refDB.runTransactionBlock({ (currentData:FIRMutableData) -> FIRTransactionResult in
        var dataToUpdate = currentData.value as? [String : Any]
        if dataToUpdate?[problem] == nil {
            dataToUpdate = [problem: 0]
            var theProblem = dataToUpdate?[problem] as? Int ?? 0
            theProblem += 1
            dataToUpdate?[problem] = 1
            currentData.value = dataToUpdate
            return FIRTransactionResult.success(withValue: currentData)
        }
        else
        {
            var theProblem = dataToUpdate?[problem] as? Int ?? 0
            theProblem += 1
            dataToUpdate?[problem] = theProblem
            currentData.value = dataToUpdate
            return FIRTransactionResult.success(withValue: currentData)
        }
        }) {(error,commited,snapshot) in
        if let error = error {
            print("errorrrrr", error.localizedDescription)
        }
    }
}
}

我的数据库结构是:

users
    I_uid
         I_problems
                   I_problem1: 1
                     problem2: 1
                     problem3: 1

problems是孩子。problem1problem2problem3是值,1是解决的次数,解决了每个问题。

我仍然认为问题是您的参考。

func uploadTheResolvedProblemsToDB(problems: [String], uid: String) {
  let refDB = FIRDatabase.database().reference().child("users").child(uid).child("problems")
  for problem in problems {
    refDB.child(problem).runTransactionBlock({ (currentData: FIRMutableData) -> FIRTransactionResult in
      var value = currentData.value as? Int
      if value == nil {
        value = 1
      } else {
        value += 1
      }
      currentData.value = value
      return FIRTransactionResult.success(withValue: currentData)
    }) { (error, comited, snapshot) in
         if let error = error {
           print("errorrrrr", error.localizedDescription)
         }
    }
  }
}

我已经对您的逻辑进行了一些编辑,但我相信这是应该使用的方式。我尚未在此上进行firebase实例,因此,如果存在一些差异,那是因为我无法完全测试它。我希望它能有所帮助:

let refDB = FIRDatabase.database().reference().child("users").child(uid).child("problems")
for problem in problems {
    refDB.runTransactionBlock({ (currentData:FIRMutableData) -> FIRTransactionResult in
        var dataToUpdate = currentData.value as? [[String : Any]]
        if var resultData = dataToUpdate {
            if dataToUpdate.keys.contains(problem) {
                if let timesResolved = dataToUpdate[problem] as? Int {
                    resultData[problem] = timesResolved + 1
                }
                else {
                    resultData[problem] = 0
                }
            }
            else {
                resultData[problem] = 0
            }
            if !resultData.keys.contains(problem) {
                resultData[problem] = dataToUpdate[problem] ?? 0
            }
            currentData.value = resultData
            return FIRTransactionResult.success(withValue: currentData)
        }
    }) {(error,commited,snapshot) in
        if let error = error {
            print("errorrrrr", error.localizedDescription)
        }
    })
}

最新更新