Ios Swift-Firebase如果ref不再存在,如何触发TransactionResult.abort()
如果我想从数据库中检索一些数据,我可以使用Ios Swift-Firebase如果ref不再存在,如何触发TransactionResult.abort(),ios,swift,firebase,firebase-realtime-database,Ios,Swift,Firebase,Firebase Realtime Database,如果我想从数据库中检索一些数据,我可以使用snapshot.exists()检查它是否存在: 但是当运行事务时,我只是更新了一些不再存在的东西,而不是得到一个错误,它更新了ref,这是我没有预料到的 1-事务应该更新的子引用: @posts @postId_123 // this post has actually been deleted -url: ... -timeStamp: ... -comments_count: 10 -2用户可以删除其中一条评论
snapshot.exists()
检查它是否存在:
但是当运行事务时,我只是更新了一些不再存在的东西,而不是得到一个错误,它更新了ref,这是我没有预料到的
1-事务应该更新的子引用:
@posts
@postId_123 // this post has actually been deleted
-url: ...
-timeStamp: ...
-comments_count: 10
-2用户可以删除其中一条评论,而无需实际查看。一旦发生这种情况,评论数量就会减少
let postsRef = Database.database().reference().child("posts").child("postId_123").child("comments_count")
postsRef.runTransactionBlock({ (mutableData: MutableData) -> TransactionResult in
var currentCount = mutableData.value as? Int ?? 0
mutableData.value = currentCount - 1
currentCount = mutableData.value as? Int ?? 0
if currentCount < 0 {
mutableData.value = 0
}
return TransactionResult.success(withValue: mutableData)
}, andCompletionBlock: { [weak self](error, completion, snap) in
if !completion || (error != nil) {
print("The value wasn't able to update")
print(error?.localizedDescription as Any)
} else {
print("The value updated")
}
})
如果可变数据的子项不再存在,如何运行TransactionResult.abort()
let postsRef = Database.database().reference().child("posts").child("postId_123").child("comments_count")
postsRef.runTransactionBlock({ (mutableData: MutableData) -> TransactionResult in
if !mutableData.exists() { // *** this check isn't real and is used just as an example ***
return TransactionResult.abort() // this is real
}
var currentCount = mutableData.value as? Int ?? 0
// ...
})
这是我发现的触发中止的唯一方法。如果有人以另一种方式发布,我将删除此答案
首先要知道的是,对于这个答案,无论您试图运行事务
的哪个子级,都必须事先有一个值,并且不能为nil。例如,如果试图对帖子的评论计数运行事务
,则在首次创建帖子时,评论计数必须与之一起创建,并设置为0(到目前为止没有评论)如果没有任何内容,则使用下面的方法注释计数将永远不会更新:
var dict = [String: Any]()
dict.updateValue(postId_123, forKey: "postId")
dict.updateValue(0, forKey: "comments_count") // *** this is important ***
dict...
let postsRef = ...child("posts").child(postId_123)
postsRef.updateChildValues(dict)
然后,当尝试对评论计数运行事务时,如果该值为nil,则该帖子被删除(这是假设除了删除整个帖子外,没有其他方法删除评论计数)。如果为nil,则运行TransactionResult.abort()
。在completionBlock
内,检查错误是否为nil
let postsRef = Database.database().reference().child("posts").child("postId_123").child("comments_count")
postsRef.runTransactionBlock({ (mutableData: MutableData) -> TransactionResult in
// if nil then the post was deleted
let checkIfCountExists = mutableData.value as? Int
if checkIfCountExists == nil {
return TransactionResult.abort()
}
// not nil then continue on
var currentCount = mutableData.value as? Int ?? 0
mutableData.value = currentCount - 1
currentCount = mutableData.value as? Int ?? 0
if currentCount < 0 {
mutableData.value = 0
}
return TransactionResult.success(withValue: mutableData)
}, andCompletionBlock: { [weak self](error, completion, snap) in
if !completion || (error != nil) {
print("The value wasn't able to update")
print(error?.localizedDescription as Any)
if error == nil {
print("*** transaction value is nil ***")
return
}
// there is a value there but something else went wrong so try again
} else {
print("The value updated")
}
})
let postsRef=Database.Database().reference().child(“posts”).child(“postId_123”).child(“comments_count”)
postsRef.runTransactionBlock({(mutableData:mutableData)->中的TransactionResult
//如果为零,则该帖子被删除
设checkIfCountExists=mutableData.value为?Int
如果checkIfCountExists==nil{
返回TransactionResult.abort()
}
//不是零然后继续
var currentCount=mutableData.value为?Int±0
mutableData.value=currentCount-1
currentCount=mutableData.value为?Int±0
如果currentCount<0{
mutableData.value=0
}
返回TransactionResult.success(带值:mutableData)
},andCompletionBlock:{[weak self](错误、完成、管理单元)输入
如果!完成| |(错误!=nil){
打印(“该值无法更新”)
打印(错误?.localizedDescription,如有)
如果错误==nil{
打印(“***交易值为零***”)
返回
}
//那里有一个值,但出现了其他错误,请重试
}否则{
打印(“更新的值”)
}
})
var dict = [String: Any]()
dict.updateValue(postId_123, forKey: "postId")
dict.updateValue(0, forKey: "comments_count") // *** this is important ***
dict...
let postsRef = ...child("posts").child(postId_123)
postsRef.updateChildValues(dict)
let postsRef = Database.database().reference().child("posts").child("postId_123").child("comments_count")
postsRef.runTransactionBlock({ (mutableData: MutableData) -> TransactionResult in
// if nil then the post was deleted
let checkIfCountExists = mutableData.value as? Int
if checkIfCountExists == nil {
return TransactionResult.abort()
}
// not nil then continue on
var currentCount = mutableData.value as? Int ?? 0
mutableData.value = currentCount - 1
currentCount = mutableData.value as? Int ?? 0
if currentCount < 0 {
mutableData.value = 0
}
return TransactionResult.success(withValue: mutableData)
}, andCompletionBlock: { [weak self](error, completion, snap) in
if !completion || (error != nil) {
print("The value wasn't able to update")
print(error?.localizedDescription as Any)
if error == nil {
print("*** transaction value is nil ***")
return
}
// there is a value there but something else went wrong so try again
} else {
print("The value updated")
}
})