'InvalidPathValidation' 同时使用 Swift 3

'InvalidPathValidation' while using Swift 3

我有一个应用程序使用 Firebase 在用户之间发送和接收消息。在将我的代码更新到新的 Swift 3 以便我可以将 iOS 10 的很酷的新功能添加到我的应用程序的过程中,我 运行 遇到了几个错误。我能够在运行时修复其中的大部分,除了这个:

Terminating app due to uncaught exception 'InvalidPathValidation', reason: '(child:) Must be a non-empty string and not contain '.' '#' '$' '[' or ']''

我不知道是什么原因造成的。用户登录时,用户的用户 ID 会打印到控制台,但只要按下登录按钮,我的应用程序就会崩溃。这个错误在我更新到 Swift 3 之前是不存在的,实际上只是在我修复了我应用程序另一部分的错误之后才出现的。

无论如何,我的应用程序中只有两个主要 类:LoginViewControllerChatViewController

这是 LoginViewController 的代码:

import UIKit
import Firebase

class LoginViewController: UIViewController {

    // MARK: Properties
    var ref: FIRDatabaseReference! // 1
    var userID: String = ""

    override func viewDidLoad() {
        super.viewDidLoad()
        ref = FIRDatabase.database().reference() // 2
    }

    @IBAction func loginDidTouch(_ sender: AnyObject) {
        FIRAuth.auth()?.signInAnonymously() { (user, error) in
            if let user = user {
                print("User is signed in with uid: ", user.uid)
                self.userID = user.uid
            } else {
                print("No user is signed in.")
            }

            self.performSegue(withIdentifier: "LoginToChat", sender: nil)

        }

    }

    override func prepare(for segue: UIStoryboardSegue, sender: AnyObject?) {
        super.prepare(for: segue, sender: sender)
        let navVc = segue.destinationViewController as! UINavigationController // 1
        let chatVc = navVc.viewControllers.first as! ChatViewController // 2
        chatVc.senderId = userID // 3
        chatVc.senderDisplayName = "" // 4
    }


}

这是我的 ChatViewController 代码:

import UIKit
import Firebase
import JSQMessagesViewController

class ChatViewController: JSQMessagesViewController {

    // MARK: Properties

    var rootRef = FIRDatabase.database().reference()
    var messageRef: FIRDatabaseReference!

    var messages = [JSQMessage]()
    var outgoingBubbleImageView: JSQMessagesBubbleImage!
    var incomingBubbleImageView: JSQMessagesBubbleImage!

    var userIsTypingRef: FIRDatabaseReference! // 1
    private var localTyping = false // 2
    var isTyping: Bool {
        get {
            return localTyping
        }
        set {
            // 3
            localTyping = newValue
            userIsTypingRef.setValue(newValue)
        }
    }
    var usersTypingQuery: FIRDatabaseQuery!


    override func viewDidLoad() {
        super.viewDidLoad()
        title = "ChatChat"
        setupBubbles()
        // No avatars
        collectionView!.collectionViewLayout.incomingAvatarViewSize = CGSize.zero
        collectionView!.collectionViewLayout.outgoingAvatarViewSize = CGSize.zero
        messageRef = rootRef.child("messages")
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        observeMessages()
        observeTyping()
    }

    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
    }


    func collectionView(collectionView: JSQMessagesCollectionView!,
                                 messageDataForItemAtIndexPath indexPath: NSIndexPath!) -> JSQMessageData! {
        return messages[indexPath.item]
    }

    func collectionView(collectionView: JSQMessagesCollectionView!,
                                 messageBubbleImageDataForItemAtIndexPath indexPath: NSIndexPath!) -> JSQMessageBubbleImageDataSource! {
        let message = messages[indexPath.item] // 1
        if message.senderId == senderId { // 2
            return outgoingBubbleImageView
        } else { // 3
            return incomingBubbleImageView
        }
    }

    override func collectionView(_ collectionView: UICollectionView,
                                 numberOfItemsInSection section: Int) -> Int {
        return messages.count
    }

    func collectionView(collectionView: JSQMessagesCollectionView!,
                                 avatarImageDataForItemAtIndexPath indexPath: NSIndexPath!) -> JSQMessageAvatarImageDataSource! {
        return nil
    }

    private func setupBubbles() {
        let factory = JSQMessagesBubbleImageFactory()
        outgoingBubbleImageView = factory?.outgoingMessagesBubbleImage(
            with: UIColor.jsq_messageBubbleBlue())
        incomingBubbleImageView = factory?.incomingMessagesBubbleImage(
            with: UIColor.jsq_messageBubbleLightGray())
    }

    func addMessage(id: String, text: String) {
        let message = JSQMessage(senderId: id, displayName: "", text: text)
        messages.append(message!)
    }

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell{
        let cell = super.collectionView(collectionView, cellForItemAt: indexPath) as! JSQMessagesCollectionViewCell

        let message = messages[indexPath.item]

        if message.senderId == senderId {
            cell.textView!.textColor = UIColor.white()
        } else {
            cell.textView!.textColor = UIColor.black()
        }

        return cell
    }

    func didPressSendButton(button: UIButton!, withMessageText text: String!, senderId: String!,
                                     senderDisplayName: String!, date: NSDate!) {

        let itemRef = messageRef.childByAutoId() // 1
        let messageItem = [ // 2
            "text": text,
            "senderId": senderId
        ]
        itemRef.setValue(messageItem as? AnyObject) // 3

        // 4
        JSQSystemSoundPlayer.jsq_playMessageSentSound()

        // 5
        finishSendingMessage()

        isTyping = false

    }

    private func observeMessages() {
        // 1
        let messagesQuery = messageRef.queryLimited(toLast: 25)
        // 2
        messagesQuery.observe(.childAdded) { (snapshot: FIRDataSnapshot!) in
            // 3
            let id = snapshot.value!["senderId"] as! String
            let text = snapshot.value!["text"] as! String

            // 4
            self.addMessage(id: id, text: text)

            // 5
            self.finishReceivingMessage()
        }
    }

    private func observeTyping() {
        let typingIndicatorRef = rootRef.child("typingIndicator")
        userIsTypingRef = typingIndicatorRef.child(senderId)
        userIsTypingRef.onDisconnectRemoveValue()

        // 1
        usersTypingQuery = typingIndicatorRef.queryOrderedByValue().queryEqual(toValue: true)

        // 2
        usersTypingQuery.observe(.value) { (data: FIRDataSnapshot!) in

            // 3 You're the only typing, don't show the indicator
            if data.childrenCount == 1 && self.isTyping {
                return
            }

            // 4 Are there others typing?
            self.showTypingIndicator = data.childrenCount > 0
            self.scrollToBottom(animated: true)
        }
    }

    override func textViewDidChange(_ textView: UITextView) {
        super.textViewDidChange(textView)
        // If the text is not empty, the user is typing
        isTyping = textView.text != ""
    }

    func collectionView(collectionView: JSQMessagesCollectionView!, attributedTextForCellBottomLabelAtIndexPath indexPath: NSIndexPath!) -> AttributedString! {
        return AttributedString(string:"test")
    }

}

请注意,所有这些代码都是在 Swift 3.

中编写的

如果你能找到任何可以帮助我解决问题并最终让我的应用程序运行的东西,我将是你最好的朋友。

提前致谢!

Swift 3.0 是目前处于测试阶段的开发人员版本,预计将于 2016 年底发布。

有些库目前可能还不可用,或者可能包含错误,因此您应该立即重新安装最新的 public 稳定版 Swift(当前为编写 v2.2)。

您自己说您已更新到 Xcode 的测试版。因此,我建议您通过重新下载 Xcode from the Mac App Store 并删除 Xcode.

的 beta 版本来重新安装 Swift 2.2

对于Swift3,等到Apple在2016年秋季发布public版本。然后,看看你的代码是否有效。


Firebase 可能还没有为 Swift 3.0 更新它的库。

在此之前,我建议您继续使用 Swift 2.2。您不希望您的应用程序由于兼容性错误而突然停止工作。

您说您下载了 Xcode 的测试版。我至少会等待 Apple 在 2016 年秋季发布 Xcode 的 public 版本,然后再次尝试您的代码。

但是,我们不知道 Firebase 何时会更新其库。备份您的代码,并在备用机器上下载 Xcode 的更新版本。如果您的代码在您的备用机器上正常工作,请将其下载到您的主机器上。或者,安装一个虚拟机,例如 VirtualBox,然后在那里试用您的应用程序。

Swift 3

Firebase : If you create your own keys, they must be UTF-8 encoded, can be a maximum of 768 bytes, and cannot contain ., $, #, [, ], /, or ASCII control characters 0-31 or 127.

要对付它们,您需要将非 firebase 字符替换为其他字符。

以下String extension将那些字符替换为space,您可以选择自己的字符。 (例如“-”)

添加:

extension String {
    func makeFirebaseString()->String{
        let arrCharacterToReplace = [".","#","$","[","]"]
        var finalString = self

        for character in arrCharacterToReplace{
            finalString = finalString.replacingOccurrences(of: character, with: " ")
        }

        return finalString
    }
}

使用:

let searchString = "Super.Hero"
let firebaseSearchString = searchString.makeFirebaseString()
// firebaseSearchString is "Super Hero" now