我在从Firebase获取数据时遇到问题。
架构是
{
title: "dog",
images: {
main: "dog.png",
others: {
0: "1.png",
1: "2.png",
2: "3.png"
}
}
}
如何将FDataSnapshot解析为swift模型?
答案 0 :(得分:4)
Firebase是NoSQL JSON数据库,没有架构,也没有表。数据存储在'树中。节点结构;父母和孩子。
您不需要解析Firebase JSON数据来访问它,您可以直接访问它。
FDataSnapshots包含.key,它是Firebase和.value中的父键。 .Value可能包含一个节点或多个节点。 Value将具有表示快照中数据的键:值对
因此,对于您的示例,您将拥有像此一样的Firebase结构
select column_value from table(mc.Get_REFTYPES@READ_MAIN_MCNAV(param1, param2, param3)));
所以,假设你想一次一个地读取每个dog_id_x节点并打印一些值。
CREATE OR REPLACE FUNCTION "MC"."Get_REFTYPES"(
param1 IN VARCHAR,
param2 IN NUMBER,
param3 IN DATE DEFAULT SYSDATE
)
RETURN RefType_T PIPELINED IS
CURSOR cur_st (
cur_param1 VARCHAR,
cur_param2 NUMBER,
cur_param3 DATE
) IS
select
TypeID
FROM ......
WHERE......... ;
t_st Types_T; -- Table variable to store return values
BEGIN
OPEN cur_st(param1, param2 , param3 );
FETCH cur_st BULK COLLECT INTO t_st;
CLOSE cur_st;
FOR i IN 1..t_st.COUNT LOOP
PIPE ROW(t_st(i));
END LOOP;
RETURN;
END;
这将输出
dogs
dog_id_0
title: "dog"
type: "Alaskan Malamute"
images:
main: "dog.png"
others:
0: "1.png"
1: "2.png"
dog_id_1
title: "another dog"
type: "Boxer"
images:
main: "another_dog.png"
others:
0: "3.png"
1: "4.png"
dog_id_0和dog_id_1是使用Firebase childByAutoId创建的节点名称。
您可以轻松创建Dog类,并将其传递给FDataSnapshot,它将从快照中的数据填充该类。
答案 1 :(得分:1)
2017年2月更新,Swift 3 Xcode 8
由于Swift3和Firebase的许多内容在提出此问题时已发生变化,因此我将提供解析Firebase数据的更新方法:
let userID = FIRAuth.auth()?.currentUser?.uid
//I am registering to listen to a specific answer to appear
self.ref.child("queryResponse").child(userID!).observeSingleEvent(of: .value, with: { (snapshot) in
//in my case the answer is of type array so I can cast it like this, should also work with NSDictionary or NSNumber
if let snapshotValue = snapshot.value as? NSArray{
//then I iterate over the values
for snapDict in snapshotValue{
//and I cast the objects to swift Dictionaries
let dict = snapDict as! Dictionary<String, Any>
}
}
}) { (error) in
print(error.localizedDescription)
}
答案 2 :(得分:1)
您可以使用Dictionary
手动解析它,也可以使用我的library。
您案例的示例代码:
func main(){
let root=SnapshotParser().parse(snap: Snapshot, type: Root.self)
}
class Root: ParsableObject {
var title:String?=nil
var images:Images?=nil
required init(){}
func bindProperties(binder: SnapshotParser.Binder) {
binder.bindField(name: "title", field: &title)
binder.bindObject(name: "images", field: &images)
}
}
class Images: ParsableObject {
var main:String?=nil
var others:[Int:String]?=nil
required init(){}
func bindProperties(binder: SnapshotParser.Binder) {
binder.bindField(name: "main", field: &main)
binder.bindDictionary(name: "others", dict: &others)
}
}
答案 3 :(得分:1)
尝试玩这个:
func makeItems(from snapshot: DataSnapshot) -> [SimpleItem] {
var items = [SimpleItem]()
if let snapshots = snapshot.children.allObjects as? [DataSnapshot] {
for snap in snapshots {
if let postDictionary = snap.value as? Dictionary<String, AnyObject> {
let item = SimpleItem(parentKey: snap.key, dictionary: postDictionary)
items.append(item)
}
}
}
return items
}
func loadItems() {
firebaseService.databaseReference
.child("items")
.queryOrdered(byChild: "date")
.queryLimited(toLast: 5)
.observeSingleEvent(of: .value) { snapshot in
let items = self.makeItems(from: snapshot)
print(" \(items)")
}
}
class SimpleItem {
var parentKey: String?
var id: String?
var description: String?
init(parentKey: String, dictionary: [String : AnyObject]) {
self.parentKey = parentKey
id = dictionary["id"] as? String
description = dictionary["description"] as? String
}
}
答案 4 :(得分:0)
这将解析单个对象中的所有快照子级并将其转换为数组,您可以轻松地解析具有索引的子级数组
if let snap = snapshot.children.allObjects as? [DataSnapshot]{
print(snap)
for (index,val) in snap.enumerated(){
print("values")
print(val)
print(val.value)
}
}