I want to create a nested comment section. I am using Firebase as my database. In my app I have a comment section on each post. Logged in users have the ability to comment on a post and their comments can also be commented on, creating a nested effect.
So first I display the comments that were made to the original post. What I want to do is to go through each comment and check to see if there is a comment for that comment and if there is a comment, I want it to display directly under that comment. Just like Instagram or Facebook.
Here is a JSON example of what a nested comment would look like in Firebase
{
"author" : "patient0",
"comments" : {
"comment-487" : {
"author" : "Doctor1",
"comments" : {
"comment-489" : {
"content" : "Your internal capsule in your cerebrum was affected by the stroke",
"id" : "comment-489",
"reply_to" : "comment-487",
"reply_to_type" : "comment"
},
"comment-490" : {
"author" : "Doctor2",
"content" : "Your internal capsule is closely associated with your basal ganglia structures",
"id" : "comment-490",
"reply_to" : "comment-487",
"reply_to_type" : "comment"
}
},
"content" : "I recently had a stroke",
"id" : "comment-487",
"post_id" : "post-1069",
"reply_to" : "post-1069",
"reply_to_type" : "post"
},
"comment-491" : {
"author" : "MedStudent",
"comments" : {
"c_1531642274921" : {
"content" : "Wow! I wonder what cranial nerves were affected due to the hemorrhage",
"id" : "c_1531642274921",
"post_id" : "post-1069",
"pub_time" : 1531642274922,
"reply_to" : "comment-491",
"reply_to_type" : "comment"
}
},
"content" : "The hemorrhage was by the pons and cranial nerve 3 is by the pons, maybe the patient lost the ability to accommodate their eye sight and keep their eyes open.",
"id" : "comment-491",
"num_likes" : 0,
"post_id" : "post-1069",
"reply_to" : "post-1069",
"reply_to_type" : "post"
}
},
"content" : "I have a headache",
"id" : "post-1069",
"num_comments" : 5,
"title" : "I have a headache, should I go to the hospital",
}
As of now I am able to get the inital comments to print (the comments made directly to the post)
func loadComments(){
Database.database().reference().child("main").child("posts").child(postID!).child("comments").queryOrdered(byChild: "id").observeSingleEvent(of: .value, with: { (snapshot:DataSnapshot) in
if let postsDictionary = snapshot .value as? [String: AnyObject] {
for testingkey in postsDictionary.keys {
Database.database().reference().child("main").child("posts").child(self.postID!).child("comments").child(testingkey).child("comments").queryOrdered(byChild: "post_id").observeSingleEvent(of: .value, with: { (snapshot:DataSnapshot) in
if let postsDictionary = snapshot .value as? [String: AnyObject] {
for post in postsDictionary {
}
DispatchQueue.main.async(execute: {
self.TableView.reloadData()
})
}
})
}
for post in postsDictionary {
//main comments
self.Comments.add(post.value)
}
DispatchQueue.main.async(execute: {
self.TableView.reloadData()
})
}
})
}
I just don't know how to go through each post to check to see if there is a comment associated with it. Also if there is a comment associated with the original comment, I want it to print out in a custom cell.
I'd create a class or struct for comment, with an array of comments as a property.
class Comment {
let id: String
let author: String
var content: String
var comments: [Comment]
}
Then I'd create a TopLevelComment class as a subclass of Comment
class TopLevelComment: Comment {
// Whatever special properties you want your top level comments to have
}
You can now check if a comment is replying to a post or a comment by simply using
comment is TopLevelComment
Then you should restructure your database appropriately so you can cast it to the Comment class
For your tableView, I'd use a tableview for each top level comment, maybe even a section for each.
You can create an element for comment elements .
var commentElements = [CustomStruct]()
After creating Custom Element pull the variables from Firebase and save .
if let postsDictionary = snapshot .value as? [String: AnyObject] {
guard let comment = postsDictionary["comments"] as? NSArray else { return }
for com in comment {
guard let commentObject = com as? [String:Any] else { return }
let id = commentObject["id"]
let type = commentObject["reply_to_type"]
let replyTo = commentObject["reply_to"]
let content = commentObject["content"]
let element = CustomStruct(id:id , type:type , ....)
commentElements.append(element)
}
for post in postsDictionary {
}
DispatchQueue.main.async(execute: {
self.TableView.reloadData()
})
}
After pulling all elements , you may group based on comment Id . And you can show with Section in TableView.You sort the first element "reply_to_type" : "post"
As per your question, I believe that you are having difficulty in figuring out how to parse the JSON to a format (or view model) which can be used to display your posts and comments.
You can use the following model sample (with reworks or tweaks of your own, if needed) to parse and organize your posts and it's comments.
class Post {
var author: String?
var comments: [Post] = []
var content: String?
var id: String?
var numComments: Int?
var title: String?
init(dict: [String: AnyObject]?) {
author = dict?["author"] as? String
content = dict?["content"] as? String
id = dict?["id"] as? String
numComments = dict?["num_comments"] as? Int
title = dict?["title"] as? String
if let commentsDict = dict?["comments"] as? [String: AnyObject] {
// Sort the comments based on the id which seems to be appended to the comment key.
let commentIds = commentsDict.keys.sorted()
for id in commentIds {
let comment = commentsDict[id] as? [String : AnyObject]
comments.append(Post(dict: comment))
}
}
}
}
Usage:
//
// postDict is your dictionary object corresponding to one post.
//
// Assign your post's dictionary item to this variable.
//
var postDict: [String: AnyObject]?
// "Post" object which has recursive comments within up to any number of levels.
// Comments are also using the same model object.
// If you want to use another, you can create one with the corresponding elements.
let post = Post(dict: postDict)
P.S: The JSON structure looks to be not of a unique structure. You might want to rework on this structure to make sure that your content gets mapped neatly.
Related
I have a dictionary from type [String: Any] that looks like this
"arExtensions" : {
"images" : {
"-LmgO2yG_TWbfOM4Y8X3" : {
"imagePath" : "https://firebasestorage.googleapis.com/v0/b/gpk-citycards.appspot.com/o/ARResources%2F78E88F6D-52F0-43A3-B585-9760D19F0B81?alt=media&token=be3a664f-a94b-4ead-bea4-1197155c016e",
"position" : "bottom"
},
"-LmgO4qaMKupHZIAEoLk" : {
"imagePath" : "https://firebasestorage.googleapis.com/v0/b/gpk-citycards.appspot.com/o/ARResources%2FC7303CF9-0E86-4DC6-A5F5-2761537F0A30?alt=media&token=1f928774-8221-474b-881e-7f395e439131",
"position" : "rightMiddle"
},
"-LmgO9vLT8rEx9Ndog4S" : {
"imagePath" : "https://firebasestorage.googleapis.com/v0/b/gpk-citycards.appspot.com/o/ARResources%2F9132B5B6-E904-4BCE-B56A-0271CB901A7D?alt=media&token=bd66cd1d-494b-4a82-8d74-6e00ac8c8ae6",
"position" : "leftMiddle"
}
}
}
I want to add the images into an object.
var imageObjects: [ImageObject] = []
I know that I can get the values with the keys like this
let dictImages: [String: Any] = dictArExtensions["images"] as! [String : Any]
unfortunately I don't know the key of the children of images.
with two iteration, first to get the key, second to get the needed values i solved the problem like this
var imageKeys: [String] = []
for dict in dictImages{
print(dict.key)
imageKeys.append(dict.key)
print(dict.value)
}
for keys in imageKeys{
let dictImage: [String: Any] = dictImages[keys] as! [String : Any]
let imagePath = dictImage["imagePath"] as? String
let position = dictImage["position"] as? String
imageObjects.append(ImageObject(imagePath: imagePath!, position: position!))
}
but, it seems like a bad solution.
Is there a better or rather more professional solution to this?
You can enumerate the dictionary very simply with key and value but the key is actually unused.
And if you declare the child dictionary as [String:String] you can get rid of the type cast
for (_, value) in dictImages {
let dictImage = value as! [String:String]
let imagePath = dictImage["imagePath"]
let position = dictImage["position"]
imageObjects.append(ImageObject(imagePath: imagePath!, position: position!))
}
Please read the section about dictionaries in the Language Guide
I'm porting an android app and using firebase in android it is possible to save a format in this way.
How can i do this on Swift? I read that i can store only this kind of data
NSString
NSNumber
NSDictionary
NSArray
How can I store the obj in atomic operation?
It's correct to store every field of the user object in separate action?
Firebase on Android
mDatabaseReferences.child("users").child(user.getUuid()).setValue(user)
I generally store objects as dictionaries on firebase. If, within my application, I have a User object, and it has properties as such:
class User {
var username = ""
var email = ""
var userID = ""
var consecutiveDaysLoggedOn = Int()
}
let newUser = User()
newUser.username = "LeviYoder"
newUser.email = "LeviYoder#LeviYoder.com"
newUser.userID = "L735F802847A-"
newUser.consecutiveDaysLoggedOn = 1
I would just store those properties as a dictionary, and write that dictionary to my firebase database:
let userInfoDictionary = ["username" : newUser.username
"email" : newUser.email
"userID" : newUser.userID
"consecutiveDaysLoggedOn" : newUser.consecutiveDaysLoggedOn]
let ref = Database.database().reference.child("UserInfo").child("SpecificUserFolder")
// ref.setValue(userInfoDictionary) { (error:Error?, ref:DatabaseReference) in
ref.setValue(userInfoDictionary, withCompletionBlock: { err, ref in
if let error = err {
print("userInfoDictionary was not saved: \(error.localizedDescription)")
} else {
print("userInfoDictionary saved successfully!")
}
}
Does that address your question?
I come with a little extension used to convert a standard SwiftObject in readable dictionnary for FireStore:
extension Encodable {
var toDictionnary: [String : Any]? {
guard let data = try? JSONEncoder().encode(self) else {
return nil
}
return try? JSONSerialization.jsonObject(with: data, options: .allowFragments) as? [String: Any]
}
}
For example, in my model:
struct Order: Encodable {
let symbol: String
let shares: Int
let price: Float
let userID: String
}
Called with this line:
let dictionnary = order.toDictionnary
This is the kind of dictionary generated
4 elements
▿ 0 : 2 elements
- key : "symbol"
- value : FP.PAR
▿ 1 : 2 elements
- key : "shares"
- value : 10
▿ 2 : 2 elements
- key : "userID"
- value : fake_id
▿ 3 : 2 elements
- key : "price"
- value : 100
Use
self.ref.child("users").child(user.uid).setValue(["username": username])
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
Edit (I hope this is more specific):
I can't seem to get the data from firebase to show up the page when i run the app:
When a notification is saved under a child node on firebase, an 'objectId' is printed - I want to grab the data under the node that matches this objectId under another node called 'pinion', a snippet of the JSON firebase structure is:
"notification" : {
"Gmg1ojNoBiedFPRNSL4sBZz2gSx2" : {
"-L_xNVcs7f3RhuLAcg7j" : {
"from" : "Gmg1ojNoBiedFPRNSL4sBZz2gSx2",
"objectId" : "-L_xNVcfZavjGFVv6iGs",
"timestamp" : 1552586771,
"type" : "pinion"
},
"pinions" : {
"Gmg1ojNoBiedFPRNSL4sBZz2gSx2" : {
"-L_xNVcfZavjGFVv6iGs" : {
"option A" : "Aaaa",
"option B" : "Cccc",
"question" : "Four",
"selectedId" : "FoFQDAGGX9hntBiBdXYCBHd8yas2",
"uid" : "Gmg1ojNoBiedFPRNSL4sBZz2gSx2"
},
"users" : {
"Gmg1ojNoBiedFPRNSL4sBZz2gSx2" : {
"email" : "eeee#gmail.com",
"fullname" : "Eeee",
"profileImageUrl" : "https://firebasestorage.googleapis.com/v0/b/pinion-4896b.appspot.com/o/profile_image%2FGmg1ojNoBiedFPRNSL4sBZz2gSx2?alt=media&token=209e57ca-b914-4023-8f85-fadfae7b7407",
},
Would appreciate any help and let me know if you need any other information - thank you in advance :)
UPDATE:
It's working for the question and answers but I am getting an error "Unable to infer closure type in the current context" when calling the image:
#IBOutlet weak var senderProfileImage: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
showImageOfSender()
}
var ref = Database.database().reference()
var userId = "" //this was made equal to the autoId under pinion, under the users ID in another view controller
func showImageOfSender() {
var senderPhoto = [String]()
guard let uid = Auth.auth().currentUser?.uid else {
return
}
let senderId = ref.child("pinions").child(uid).child(userId).child("uid")
ref.child("users").child(senderId).observeSingleEvent(of: .value, with: { snapshot in
//error is in the above line
let senderImage = snapshot.childSnapshot(forPath: "profileImageUrl").value as! String
senderPhoto.append(senderImage)
let senderImageUrl = URL.init(string: senderImage)
self.senderProfileImage.sd_setImage(with: senderImageUrl)
})
}
I think this is what you mean, first import Firebase:
import Firebase
then get the database reference:
class PinionNotificationsViewController: UIViewController {
var ref: DatabaseReference!
...
}
then your function can look like this if you know the UID:
func showQuestionAndAnswers() {
let uid = userId
ref = ref.child("pinions").child(uid)
ref.observeSingleEvent(of: .value, with: { (snapshot) in
// Get user value
let value = snapshot.value as? NSDictionary
let optionA = value?["option A"] as! String
let optionB = value?["option B"] as! String
print(optionA) // <- prints: "Bbbb"
print(optionB) // <- prints: "Dddd"
})
}
If you don't know the uid you can use
ref = ref.child("pinions")
ref.observe(.childAdded) { (snapshot) in
// Get user value
let value = snapshot.value as? NSDictionary
let optionA = value?["option A"] as! String
// then you can append these values into an array it will go through all of them eg:
self.answersArray.append(optionA) // answers array will contain all optionA values from all children in the database under `"pinions"`
}
This way in my experience would match the database order so in your array all options would be in the order of their children as they are in the database.
Hope this helps!
Routinely in my various projects, I have to deal with iterating over hierarchical data. Being as common as it is, it always frustrated me that I had to write so much boilerplate code to do it.
Well thanks to Swifts ability to write custom Sequence classes, I decided to see if I could write one that would achieve this goal in a reusable fashion. Below is my result.
I decided to post this here per Jeff Atwood's [own comments on encouraging posting your own answers][1] where he says...
It is not merely OK to ask and answer your own question, it is explicitly encouraged [...] I do it all the time!
As such, I'm providing this solution here in hopes of helping others when they come to search this site.
Enjoy! :)
As stated above, I wrote a class that allows you to iterate over a hierarchical set of data, while keeping that hierarchy in order. You do this by specifying one or more root elements (either via an array or a variadic), and a closure that returns the children for a given element.
Since it's implemented as a generic, you can specify an explicit type to use if you know the hierarchy is homogenous, but if not, specify Any for the type, then in the closure, perform the logic to determine what child type it is.
In addition, the implementation, via recursion, not only returns things in the correct hierarchical order, but it also returns a level so you know how deep the items are. If you don't care about the level, simply append .map{ $0.item } when initializing the sequence to extract the items directly.
Here's the code for the custom hierarchical sequence...
struct HierarchicalSequence<TItem> : Sequence {
typealias GetChildItemsDelegate = (TItem) -> [TItem]?
init(_ rootItems:TItem..., getChildItems: #escaping GetChildItemsDelegate){
self.init(rootItems, getChildItems: getChildItems)
}
init(rootItems:[TItem], getChildItems: #escaping GetChildItemsDelegate){
self.rootItems = rootItems
self.getChildItems = getChildItems
}
let rootItems : [TItem]
let getChildItems : GetChildItemsDelegate
class Iterator : IteratorProtocol {
typealias Element = (level:Int, item:TItem)
init(level:Int, items:[TItem], getChildItems: #escaping GetChildItemsDelegate){
self.level = level
self.items = items
self.getChildItems = getChildItems
}
let level : Int
let items : [TItem]
let getChildItems : GetChildItemsDelegate
private var nextIndex = 0
var childIterator:Iterator?
func next() -> Element? {
// If there's a child iterator, use it to see if there's a 'next' item
if let childIterator = childIterator {
if let childIteratorResult = childIterator.next(){
return childIteratorResult
}
// No more children so let's clear out the iterator
self.childIterator = nil
}
if nextIndex == items.count {
return nil
}
let item = items[nextIndex]
nextIndex += 1
// Set up the child iterator for the next call to 'next' but still return 'item' from this call
if let childItems = getChildItems(item),
childItems.count > 0 {
childIterator = Iterator(
level : level + 1,
items : childItems,
getChildItems : getChildItems)
}
return (level, item)
}
}
func makeIterator() -> Iterator {
return Iterator(level: 0, items: rootItems, getChildItems: getChildItems)
}
}
Let's see an example of how to use it. First, let's start with some JSON data...
public let jsonString = """
[
{
"name" : "Section A",
"subCategories" : [
{
"name" : "Category A1",
"subCategories" : [
{ "name" : "Component A1a" },
{ "name" : "Component A1b" }
]
},
{
"name" : "Category A2",
"subCategories" : [
{ "name" : "Component A2a" },
{ "name" : "Component A2b" }
]
}
]
},
{
"name" : "Section B",
"subCategories" : [
{
"name" : "Category B1",
"subCategories" : [
{ "name" : "Component B1a" },
{ "name" : "Component B1b" }
]
},
{
"name" : "Category B2",
"subCategories" : [
{ "name" : "Component B2a" },
{ "name" : "Component B2b" }
]
}
]
}
]
"""
Here's the models and code to load that data
class Category : Codable {
let name : String
let subCategories : [Category]?
}
public let jsonData = jsonString.data(using: .utf8)!
var rootCategories = try! JSONDecoder().decode([Category].self, from: jsonData)
Here's how you use the sequence getting all the categories along with their depths...
let allCategoriesWithDepth = HierarchicalSequence(rootItems:rootCategories){ $0.subCategories }
for (depth, category) in allCategoriesWithDepth {
print("\(String(repeating: " ", count: depth * 2))\(depth): \(category.name)")
}
And finally, here's the output...
0: Section A
1: Category A1
2: Component A1a
2: Component A1b
1: Category A2
2: Component A2a
2: Component A2b
0: Section B
1: Category B1
2: Component B1a
2: Component B1b
1: Category B2
2: Component B2a
2: Component B2b
Enjoy!
I have a data structure in firebase which I am showing that data on a tableview. I am getting the data from firebase. The tableview sections are hard coded like Motivation, Success etc... What is the best way to parse this data so that, when I add a new section in firebase console, it will add that section and data for the section on the tableview, without me hard coding the sections? Any help in the right direction would be appreciated, read the firebase doc but can't seem to figure it out.
Data Structure
{
"categories" : {
"motivation" : {
"one" : {
"name" : "Bob",
"title" : "Get up stand up"
},
"two" : {
"name" : "Arsitotle",
"title" : "Great philosopher"
}
},
"success" : {
"one" : {
"name" : "Les",
"title" : "You're great"
},
"three" : {
"name" : "Bob",
"title" : "One love"
},
"two" : {
"name" : "Wayne",
"title" : "You will be great"
}
}
}
}
** Retrieving the data**
ref.child("categories/motivation").observe(.childAdded, with: {(snapshot:DataSnapshot) in
if let values = snapshot.value as? [String:String] {
self.motivationDictionary.insert(values, at: 0)
}
})
}
ref.child("categories/success").observe(.childAdded, with: {(snapshot:DataSnapshot) in
if let values = snapshot.value as? [String:String] {
self.successDictionary.insert(values, at: 0)
}
})
}
I know this isn't the best way, but it works. Kind of redundant, but I am new to firebase and databases.
So you are having a fixed number of dictionaries where each dictionary has a name (motivationDictionary, successDictionary).
Instead you could have a dictionary of dictionaries (like in your data structure), such that the top dictionary contains categories, and under each key you have a dictionary of values for that category, i.e. the new self.categories["motivation"] is the same as the old self.motivationDictionary and so on.
This should work, but it is not the best practice to just operate on raw dictionaries and strings. This approach might be typical for some other languages (like Lisps), but not the way to go for Swift.
In Swift you should define your model classes, and parse your DataSnapshot as instances of those classes. For example if you start from:
struct Item {
let name: String
let title: String
}
class Category {
let name: String = ""
var items: [String: Item] = [:]
}
class TableDataModel {
var sections: [Category] = []
}
Then inside your observe, you can fill your TableDataModel, and then reload the table from the model. This way the Swift compiler helps you more to ensure that your program is correct, and the code is somewhat clearer.