Firebase:每次循环遍历数组

时间:2017-03-08 19:34:10

标签: ios swift firebase firebase-realtime-database

我试图遍历数组并运行事务来上传Firebase上的每个项目,但我不确定我想做什么是可能的。

这个想法是:我有一系列问题["problemType1", "problemType2", ... "problemType10"],我给用户n时间来解决它。最后,我将解决的问题放在Array中并将它们上传到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是已解决的次数,每个问题都已解决。

2 个答案:

答案 0 :(得分: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)
         }
    }
  }
}

答案 1 :(得分:1)

我已经对你的逻辑进行了一些编辑,但我相信这是应该如何运作的。我没有在此运行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)
        }
    })
}