我正在开发一个聊天应用,用户应该收到有关其联系人新消息的通知。此通知消息还应包括未读消息的数量。由于发件人和收件人都可以更新此信息,因此首选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)
}
}
}
}
我的代码:
text
修改
与我的第一个答案的期望相反,错误仍然存在。我认为它与连接有关?例如。当没有良好的连接时,有时需要更长的时间来运行交易?但有时当我坐在路由器旁边时也会发生这种情况。其他节点被写入,但不会写入事务。在这些情况下重新启动应用程序后,它再次开始工作。所以我觉得引擎盖下有什么问题。
我非常感谢这个问题的解决方案。接收器有时无法收到有关新消息的通知的聊天应用程序是不行的。
我也熟悉解决方法:当你想增加一个计数器时,实际上是否需要交易?我可以使用senderId
更新timestamp
,setValue
或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)
}
}
}
等其他数据,但当两个用户都尝试设置子节点的值时,这会导致数据损坏同时,不是吗?
这是我的最新代码:
MaintainFormDto
答案 0 :(得分:4)
好吧,显然Firebase SDK中存在一个错误。即使更新成功,有时也无法执行completionBlock
的回调。我删除了 self.ref.updateChildValues(childUpdates)
JSQSystemSoundPlayer.jsq_playMessageSentSound()
self.finishSendingMessage()
self.updateOpenChats(text)
,现在它完美无缺。
<?php
function DecryptId($id) {
$first_digit = substr($id, 0,1);
if ( $first_digit == 0 ) {
return 'yes';
} else {
return 'no';
}
}
$id = '014';
echo DecryptId($id);
?>
修改强> 看到更新的问题,问题仍然存在。