gpt4 book ai didi

ios - 火力地堡/iOS : runTransactions sometimes doesn't work

转载 作者:搜寻专家 更新时间:2023-10-30 22:13:52 24 4
gpt4 key购买 nike

我正在开发一个聊天应用程序,用户应该在其中收到有关来自其联系人的新消息的通知。此通知消息还应包括未读消息的数量。因为发送方和接收方都可以更新此信息 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 更新其他数据,例如 textsenderIdtimestamp,但这会导致数据损坏,当两个用户同时尝试设置子节点的值时,不是吗?

这是我最新的代码:

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com