1

I have some user comments stored in a database (parse-server) that I would like to would like to display on my viewController's viewDidLoad(). I can easily pull the comment objects as follows: super.viewDidLoad()

    func query(){
        let commentsQuery = PFQuery(className: "Comments")
        commentsQuery.whereKey("objectId", equalTo: detailDisclosureKey)
        commentsQuery.findObjectsInBackground { (objectss, error) in
            if let objects = objectss{
                if objects.count == 1{
                    for object in objects{
                        self.unOrderedComments.append(object)
                    }
                }
            }
        }

    }

This query dumps all of the of the comments in the unOrederedComments array. Each comment is added to the database with a createdAt property automatically being added relating the exact time of its creation. This property is a string with (as an example) the form: "2017-08-13T19:31:47.776Z" (the Z at the end is at the end of every string... not exactly sure why its there but its constant). Now, each new comment is added in order to the top of database and thus any queried result should be in order regardless. However, I would like to make sure of this by reordering it if necessary. My general thought process is to use .sorted, but I cannot figure out how to apply this to my situation

func orderComments(unOrderComments: [PFObject]) -> [PFObject]{
    let orderedEventComments = unOrderedEventComments.sorted(by: { (<#PFObject#>, <#PFObject#>) -> Bool in
            //code
        })
}

This is the generic set up but I cannot, despite looking up several examples online figure out what to put in the <#PFObject#>'s and in the //code. I want to order them based on the "createdAt" property but this is not achieved via dot notation and instead requires PFObject["createdAt"] and using this notation keeps leading to error. I feel as so though I may need to set up a custom predicate but I do not know how to do this.

Runeaway3
  • 1,439
  • 1
  • 17
  • 43

1 Answers1

2

I was in the same situation, what I did was to first create an array of structs with the data I downloaded where I turned the string createdAt into a Date, then used this function:

dataArrayOrdered = unOrderedArray.sorted(by: { $0.date.compare($1.date) == .orderedAscending})

(.date being the stored Date inside my array of strcuts)

Try this code, notice that I assumed you have a variable name called ["Comments"] inside your Parse database, so replace if necessary. Also, I realised that createdAt it's in Date format, so there was no need to change it from String to Date, chek if it works the same for you, if it doesn't refer to this: Swift convert string to date.

    struct Comment {
        var date = Date()
        var comment = String()
    }

    var unOrderedComments: [Comment] = []
    var orderedComments = [Comment]()


    override func viewDidLoad() {
    super.viewDidLoad()

        query()
  }


    func query(){
        let commentsQuery = PFQuery(className: "Comments")
        commentsQuery.findObjectsInBackground { (objectss, error) in

            if let objects = objectss{
                if objects.count >= 1{
                    for object in objects{
                        let newElement = Comment(date: object.createdAt!, comment: object["Comments"] as! String)
                        self.unOrderedComments.append(newElement)
                        print(self.unOrderedComments)
                    }
                }
                self.orderedComments = self.unOrderedComments.sorted(by: { $0.date.compare($1.date) == .orderedAscending})
                print(self.orderedComments)
            }
        }
    }
Enrique
  • 293
  • 1
  • 2
  • 15
  • I decided to create a struct literally as I got the notification for this answer. However, when trying to access the struct (called `Comment`), ie. when setting the parameters of the pulled object to the new variable (`var comment = self.Comment(date: object["Date"])`) (object being the value from the first code block I have in the question... the pulled object from the database) I get the error that the static member Comment cannot be used on the instance type of (myViewController). Did you run into this? How did you get around it? – Runeaway3 Aug 18 '17 at 19:08
  • Edited my answer, see if it helps – Enrique Aug 18 '17 at 20:26