In my project app I use Firebase Storage and Database simultaneously. I would like to use transactions concept, however I have never used it before so it's not obvious for me. Users are allowed to upload a file to the server. I track these uploads in database (assigning url's of the uploaded file to the user). So I perform 2 actions at the same time:
uploading the file
updating database
I wanna be sure that if any of those actions fails, none is performed (e.g. Internet connection is down). I need some tips on how to handle exceptions like the one mentioned before and how to implement it in appropriate way as it is crashing at the moment. Here is the code I have:
newRef.runTransactionBlock({ (currentData: MutableData) ->
TransactionResult in
let uploadTask = ref.putData(contents as Data, metadata:
self.myMetadata, completion: { (metadata, error) in
if error != nil {
...
}
DataService.instance.usersRef.observeSingleEvent(of: .value) {
(snapshot: DataSnapshot) in
...
_ = DataService.instance.usersRef.child("\
(key)/profile/myURLS").updateChildValues([ strEncoded as!
AnyHashable : downloadURL])
}
}
return TransactionResult.success(withValue: currentData)
})
{ (error, committed, snapshot) in
if let error = error {
print(error.localizedDescription)
}
}
Thanks in advance!