I'm new to CosmicMind and have questions about the data process.
In the CosmicMind CardTableView Sample, the data are read from SampleData of Entity type.
Q1: Is there a way I can load my Firebase Data to Entity? And if so, how to do it? Looks like shouldn't be difficult, as it's JSON already. I'm using Swift 3.2
Q2: Is there a way to modal the Entity?
Thanks a lot!!
Welcome to CosmicMind :)
The easiest way would be to create an import function that takes in your JSON data and maps the key value pairs to properties in an Entity. If there is a type specifier in Firebase, then you can set that as the Entity type. I don't use Firebase, so I can't describe the setup 100%, but here is an example of what you would do, assuming Firebase gives you a unique ID.
func importFirebase(data: [[String: Any]]) {
let graph = Graph()
let search = Search<Entity>(graph: graph).for(types: "User")
let users = search.sync()
var index = [Int: Entity]()
for user in users {
if let id = user["id"] as? Int {
index[id] = user
}
}
for json in data {
if let id = json["id"] as? Int {
var entity: Entity
if nil == index[id] {
entity = Entity(type: "User")
} else {
entity = index[id]!
}
for (k, v) in json {
entity[k] = v
}
}
}
graph.sync()
}
let data = [
["type": "User", "id": 1, "name": "Daniel", "age": 33],
["type": "User", "id": 2, "name": "Adam", "age": 28],
["type": "User", "id": 3, "name": "Sarah", "age": 23]
]
importFirebase(data: data)
This is not necessarily the optimal solution, it should give you an idea though.
Hope it helps, all the best!