GVKun编程网logo

Firebase 读取和保存数据问题 (Swift)(firebase storage)

24

对于想了解Firebase读取和保存数据问题(Swift)的读者,本文将提供新的信息,我们将详细介绍firebasestorage,并且为您提供关于Firebase参考对象不会保存到SwiftUI中的

对于想了解Firebase 读取和保存数据问题 (Swift)的读者,本文将提供新的信息,我们将详细介绍firebase storage,并且为您提供关于Firebase参考对象不会保存到SwiftUI中的变量、FIREBASE数据库 – 将唯一键存储到子节点(Swift / IOS)、Firebase配置失败 – Swift、IOS Swift SwiftUI Firebase “未找到默认存储桶您是否正确配置了 Firebase 存储?”的有价值信息。

本文目录一览:

Firebase 读取和保存数据问题 (Swift)(firebase storage)

Firebase 读取和保存数据问题 (Swift)(firebase storage)

这可能会帮助您前进。

尽可能长时间地将数据保留在 DataSnapshots 中是个好主意 - 例如转换为 Dictionary 会使处理变得更加复杂。

此外,排序也很重要,因为您可以看到第一步是获取子节点并将它们转换为数组以保持顺序

.observeSingleEvent 在这里使用,因为我只读取食物一次,没有留下未来变化的观察者。

func readAllFoods() {
    let ref = self.ref.child("foods") //self.ref points to MY firebase.
    ref.observeSingleEvent(of: .value,with: { snapshot in
        let allRestaurantsSnap = snapshot.children.allObjects as! [DataSnapshot] //contains all child nodes of food
        for foodSnap in allRestaurantsSnap { //iterate over each restaurant node
            let restaurantName = foodSnap.key //arborGrill,burgerKing etc
            let foodName = foodSnap.childSnapshot(forPath: "foodName").value as? String ?? "No food name"
            print(foodName)
        }
    })
}

我也会劝阻您不要在 NoSQL 数据库中使用数组。它们可能非常有限并且难以使用,就像您需要更改数组一样,必须读入、更改、删​​除和写回数组。

所以不是这个

reviewString
  0: Some Review
  1: Another Review

这样做

reviews
   -9as99idjk4ii9jas09 //created with childByAutoId
      review: "Awesome restaurant"
      by_user: "uid_1234"
      date: 20210131
   -ajsijo9fj9dkkfg00d
      review: "Top notch cuizine"
      by_user: "uid_5678"
      date: 20210210

Firebase参考对象不会保存到SwiftUI中的变量

Firebase参考对象不会保存到SwiftUI中的变量

那完全是关于异步行为。尝试进行以下操作:

func makeRequestToFirebase(completion: @escaping ([String]) -> Void) {
    ref.observeSingleEvent(of: .value,with: {
        snapshot in
        var bannedNamesList = [String]()
        for bannedNames in snapshot.children {
            bannedNamesList.append((bannedNames as AnyObject).key)
        }
        listOfBannedNames = bannedNamesList
        print(listOfBannedNames)
        completion(listOfBannedNames) // - that will wait until the list of names arrives
    })
}

然后使用功能:

makeRequestToFirebase() { names in
    print(names)
    workWithNames(names)
}

现在您可以根据需要使用它,例如:

func workWithNames(names: [String]) {
    for name in names {
        if name == "Alexander" {
            print("One more Alexander found")
        }
}

也请进一步了解escaping closures in Swift。

FIREBASE数据库 – 将唯一键存储到子节点(Swift / IOS)

FIREBASE数据库 – 将唯一键存储到子节点(Swift / IOS)

我试图在调用childByAutoId时存储生成的唯一键.理论上,它可以帮助映射需要在我的应用程序中稍后发生的指定子项的更新或更改.

我对Firebase的分层数据库相当新,如果下面描述的方法不正确,请不要犹豫,给出替代方案.

我对Firebase很新,并且拥有一个多人应用,我的结构目前如下所示:

"HTGames" : {

"-KFGX5H3rLSnpPvupakm" : {
  {
    "Red" : 1,"Green" : 3,"Blue" : 2,"GameLimit" : 1,"P1Name" : 3,"P2Name" : 2,"P3Name" : 1,"P1Points" : 3,"P2Points" : 2,"P3Points": 3,"numberOfPlayers" : 1.
    "gameStart": true
  }
},"-X5Hhgljh3rLSnpPvum" : {
  {
    "Red" : 1,"numberOfPlayers" : 1.
    "gameStart": true
  }}}
}

下面的Swift方法调用…

func startNewGame(){
//Database reference object
    let databaseRef = FIRDatabase.database().reference()

    let gaMetable : [String : AnyObject] = ["Green": "","Red": "","Blue": "","GameTypePoint": "","P1Name": "","P2Name": "","P3Name": "","P1Points": "","P2Points": "","P3Points": "","NumOfPlayers": "","GameStart": ""]

//Auto generates unique ID and creates child under "HTGame" parent
    databaseRef.child("HTGame").childByAutoId().setValue(gaMetable)

//**********I would like to store key locally here*********//
    //let key =
}

解决方法

如果你保留对它的引用,你可以从新孩子那里获取钥匙:
let newRef = databaseRef.child("HTGame").childByAutoId()
newRef.setValue(gaMetable)

let key = newRef.key

有关此内容的更多信息,请参见保存数据指南:https://firebase.google.com/docs/database/ios/save-data#append_to_a_list_of_data

Firebase配置失败 – Swift

Firebase配置失败 – Swift

在我的应用程序中,我成功使用了Firebase,在AppDelegate中我进行了设置:
// ### Initialize Firebase
FIRApp.configure()

现在我对相关目标进行一些单元测试,当我启动它时,我得到错误:

2017-04-14 14:53:22.351 MyProject[28753] <Error> [Firebase/Core][I-COR000004] App with name __FIRAPP_DEFAULT does not exist.
2017-04-14 14:53:22.354 MyProject[28753] <Error> [Firebase/Messaging][I-IID001000] Firebase is not set up correctly. Sender ID is nil or empty.
2017-04-14 14:53:22.356 MyProject[28753] <Notice> [Firebase/Analytics][I-ACS023007] Firebase Analytics v.3800000 started
2017-04-14 14:53:22.356 MyProject[28753] <Notice> [Firebase/Analytics][I-ACS023008] To enable debug logging set the following application argument: -FIRAnalyticsDebugEnabled
2017-04-14 14:53:22.381 MyProject[28753:712475] *** Terminating app due to uncaught exception 'com.firebase.instanceid',reason: 'Could not configure Firebase InstanceID. Google Sender ID must not be nil or empty.'

版本:

Firebase/Core (3.16.0)
Firebase/Messagging (3.16.0)

任何建议?

我刚刚注意到它开始发生在我身上,我的travis版本因Firebase 3.16而失败。我降级到3.7.1,这是我以前在项目中的版本,它再次运行。

我没有时间去研究它,但这是一个快速修复。它可能是Firebase错误,或者它们可能已经改变了某些内容,现在设置也不同了。

编辑:显然回滚到3.15工作得很好。

IOS Swift SwiftUI Firebase “未找到默认存储桶您是否正确配置了 Firebase 存储?”

IOS Swift SwiftUI Firebase “未找到默认存储桶您是否正确配置了 Firebase 存储?”

通过将其与 Firebase 项目断开连接并将其重新连接到新的 Firebase 项目来解决该问题。另外重新下载 firebase .plist 文件可能已经完成了(确保将 .plist 文件添加回 xcode 时,它​​的名称末尾没有 (2) 或 (3)。否则 xcode 获胜不认识它。)还添加了“Firebase/Analytics”吊舱,不确定如何,但这可能有所帮助。希望这对正在寻找解决方案的人有所帮助。

关于Firebase 读取和保存数据问题 (Swift)firebase storage的介绍已经告一段落,感谢您的耐心阅读,如果想了解更多关于Firebase参考对象不会保存到SwiftUI中的变量、FIREBASE数据库 – 将唯一键存储到子节点(Swift / IOS)、Firebase配置失败 – Swift、IOS Swift SwiftUI Firebase “未找到默认存储桶您是否正确配置了 Firebase 存储?”的相关信息,请在本站寻找。

本文标签: