- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在开发一个聊天应用程序,用户应该在其中收到有关来自其联系人的新消息的通知。此通知消息还应包括未读消息的数量。因为发送方和接收方都可以更新此信息 runTransaction
是首选。不幸的是,有时它不起作用。它感觉“卡住”,然后在一段时间后再次开始工作。 privateChats
节点(见下文)始终使用最新消息进行更新,但 openChatMessages
节点不会。
如果在短时间内发送许多消息,是否会发生这种情况,即 runTransactions
对相同的 ref
执行得太频繁?
我的数据结构:
privateChats
$userId
$chatId
$messageId
text
timestamp
senderId
senderEmail
senderName
// this node contains information about open chats
// like last message and counter for unread messages
openChatMessages
$userId
$chatId
text
timestamp
senderId
senderEmail
senderName
counter
我的代码:
class ChatViewController: JSQMessagesViewController {
var user: FIRUser!
var ref: FIRDatabaseReference!
var chatRef: FIRDatabaseReference!
var senderOpenChatRef: FIRDatabaseReference!
var receiverOpenChatRef: FIRDatabaseReference!
// the following variables will be set before ChatViewController appears
var chatId: String?
var receivId: String?
var receiverEmail: String?
var receiverName: String?
override func viewDidLoad() {
super.viewDidLoad()
self.user = FIRAuth.auth()?.currentUser!
self.ref = FIRDatabase.database().reference()
self.chatRef = self.ref.child("privateChats").child(self.user.uid).child(self.chatId!)
self.senderOpenChatRef = self.ref.child("openChatMessages").child(self.user.uid).child(self.chatId!)
self.receiverOpenChatRef = self.ref.child("openChatMessages").child(self.receiverId!).child(self.chatId!)
}
func sendMessage(text: String) {
var messageObject = [String: AnyObject]()
messageObject["text"] = text
messageObject["timestamp"] = FIRServerValue.timestamp()
messageObject["senderEmail"] = self.user.email
messageObject["senderName"] = self.user.displayName
messageObject["senderId"] = self.user.uid
let messageId = self.ref.child("privateChats").child(self.user.uid).child(self.chatId!).childByAutoId().key
let childUpdates = [
"/privateChats/\(self.user.uid)/\(self.chatId!)/\(messageId)": messageObject,
"/privateChats/\(self.receiverId!)/\(self.chatId!)/\(messageId)": messageObject
]
self.ref.updateChildValues(childUpdates, withCompletionBlock: { (error, ref) -> Void in
if error != nil {
print("childUpdates error:\(error)")
return
}
JSQSystemSoundPlayer.jsq_playMessageSentSound()
self.finishSendingMessage()
self.updateOpenChats(text)
})
}
func updateOpenChats(text: String) {
// update the receivers openChatObject with increasing the counter
self.receiverOpenChatRef.runTransactionBlock({ (currentData: FIRMutableData) -> FIRTransactionResult in
var openChatObject = [String: AnyObject]()
// update openChatObject with the latest information from currentData
if currentData.hasChildren() {
openChatObject = currentData.value as! [String: AnyObject]
}
openChatObject["text"] = text
openChatObject["timestamp"] = FIRServerValue.timestamp()
openChatObject["senderEmail"] = self.user.email
openChatObject["senderName"] = self.user.displayName
openChatObject["senderId"] = self.user.uid
var counter = openChatObject["counter"] as? Int
if counter == nil {
counter = 1
} else {
counter = counter! + 1
}
openChatObject["counter"] = counter
currentData.value = openChatObject
return FIRTransactionResult.successWithValue(currentData)
}) { (error, committed, snapshot) in
if let error = error {
print("updateOpenChats: \(error.localizedDescription)")
}
}
// update your (the sender's) openChatObject with setting the counter to zero
self.senderOpenChatRef.runTransactionBlock({ (currentData: FIRMutableData) -> FIRTransactionResult in
var openChatObject = [String: AnyObject]()
// update openChatObject with the latest information from currentData
if currentData.hasChildren() {
openChatObject = currentData.value as! [String: AnyObject]
}
openChatObject["text"] = text
openChatObject["timestamp"] = FIRServerValue.timestamp()
openChatObject["senderEmail"] = self.receiverEmail
openChatObject["senderName"] = self.receiverName
openChatObject["senderId"] = self.receiverId
openChatObject["counter"] = 0
currentData.value = openChatObject
return FIRTransactionResult.successWithValue(currentData)
}) { (error, committed, snapshot) in
if let error = error {
print(error.localizedDescription)
}
}
}
}
编辑:
与我对第一个答案的预期相反,错误仍然存在。我认为这与连接有关?例如。当没有良好的连接时,有时需要更长的时间来运行交易?但有时当我坐在路由器旁边时也会发生这种情况。其他节点被写入,但没有写入交易的节点。在这些情况下重新启动应用程序后,它会再次开始工作。所以我猜引擎盖下有问题。
我非常感谢这个问题的解决方案。接收者有时不会收到有关新消息的通知的聊天应用是不行的。
我也接受变通方法:当您想要增加计数器时,是否真的需要事务?我可以使用 setValue
更新其他数据,例如 text
、senderId
或 timestamp
,但这会导致数据损坏,当两个用户同时尝试设置子节点的值时,不是吗?
这是我最新的代码:
func sendMessage(text: String?, video: NSURL?, image: UIImage?) {
var messageObject = [String: AnyObject]()
messageObject["text"] = text
messageObject["timestamp"] = FIRServerValue.timestamp()
messageObject["senderEmail"] = self.user.email
messageObject["senderName"] = self.user.displayName
messageObject["senderId"] = self.user.uid
func completeSending() {
let messagesRef = self.ref.child("messages").child(self.chatId!).childByAutoId()
messagesRef.setValue(messageObject)
JSQSystemSoundPlayer.jsq_playMessageSentSound()
if let _ = image {
self.updateOpenChats("📷 Photo")
} else if let text = text {
self.updateOpenChats(text)
}
self.finishSendingMessageAnimated(true)
}
if let image = image { // if an image is being sent
let data: NSData = UIImageJPEGRepresentation(image, 0.37)!
let fileName = "image_\(NSDate().timeIntervalSince1970).jpg"
let chatImagesRef = storageRef.child("chatImages/\(self.chatId!)/\(fileName)")
let uploadTask = chatImagesRef.putData(data, metadata: nil) { metadata, error in
if (error != nil) {
print(error)
return
}
}
uploadTask.observeStatus(.Failure) { snapshot in
ProgressHUD.showError("Uploading image failed.")
}
uploadTask.observeStatus(.Success) { snapshot in
let imageUrl = snapshot.reference
messageObject["imageUrl"] = String(imageUrl)
completeSending()
}
} else { // if it's just a text message
completeSending()
}
}
func updateOpenChats(text: String) {
self.receiverChatRef.runTransactionBlock({ (currentData: FIRMutableData) -> FIRTransactionResult in
var openChatObject = [String: AnyObject]()
if currentData.hasChildren() {
openChatObject = currentData.value as! [String: AnyObject]
}
openChatObject["text"] = text
openChatObject["timestamp"] = FIRServerValue.timestamp()
openChatObject["senderEmail"] = self.user.email
openChatObject["senderName"] = self.user.displayName
openChatObject["senderId"] = self.user.uid
openChatObject["pushId"] = Database.pushId
var counter = openChatObject["counter"] as? Int
if counter == nil {
counter = 1
} else {
counter = counter! + 1
}
openChatObject["counter"] = counter
// Set value and report transaction success
currentData.value = openChatObject
return FIRTransactionResult.successWithValue(currentData)
}) { (error, committed, snapshot) in
if let error = error {
print("updateOpenChats: \(error.localizedDescription)")
}
}
self.senderChatRef.runTransactionBlock({ (currentData: FIRMutableData) -> FIRTransactionResult in
var openChatObject = [String: AnyObject]()
if currentData.hasChildren() {
openChatObject = currentData.value as! [String: AnyObject]
}
openChatObject["text"] = text
openChatObject["timestamp"] = FIRServerValue.timestamp()
openChatObject["senderEmail"] = self.receiver.email
openChatObject["senderName"] = self.receiver.name
openChatObject["senderId"] = self.receiver.uid
openChatObject["counter"] = 0
// Set value and report transaction success
currentData.value = openChatObject
return FIRTransactionResult.successWithValue(currentData)
}) { (error, committed, snapshot) in
if let error = error {
print(error.localizedDescription)
}
}
}
最佳答案
好的,显然 Firebase SDK 中存在错误。 updateChildValues
的回调有时不会执行,即使更新已成功。我删除了 completionBlock
,现在它可以正常工作了。
self.ref.updateChildValues(childUpdates)
JSQSystemSoundPlayer.jsq_playMessageSentSound()
self.finishSendingMessage()
self.updateOpenChats(text)
编辑:查看更新后的问题,问题仍然存在。
关于ios - 火力地堡/iOS : runTransactions sometimes doesn't work,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37774697/
我正在使用 Firebase 在我的应用程序中添加登录/注册。它运行良好,但现在我想检查用户在打开应用程序时是否已经注册并登录,如果是这样,则向他显示主页,否则向他显示登录页面。所以我做了一些搜索,主
我使用 flutter 。我想仅在 2020 年 4 月 26 日这一周将 Firebase 中的 CH 值更新为 false。什么时候有办法? 失败的 Ex.cord: _firestore.col
我来寻求您对我的申请中遇到的问题的帮助。 问题是这样的:新闻源上的帖子位于此捕获中的此帖子 -> [postid] 位置下的数据库中 ,当用户保存帖子时,它在数据库中的存储方式如下:用户 -> [us
我是一名优秀的程序员,十分优秀!