从按日期分析组织的组合数组

时间:2015-11-29 19:59:27

标签: ios swift parse-platform

我将PFObject数组合并到了Parse ......

array.appendContentsOf(otherArray)

如何创建这个大数组并在创建时将其组织起来?

基本上我的查询是什么,我拿出了大部分东西并改变了一些东西,所以如果你没有看到我的整个班级就有意义......

func loadStuff() {

    let postQuery = PFQuery(className: "Post")

    postQuery.orderByDescending("createdAt")

    postQuery.whereKey("type", equalTo: "world")

    postQuery.whereKey("distance", equalTo: distanceType)

    postQuery.whereKey("location", nearGeoPoint: PFGeoPoint(location: currentLocation), withinMiles: miles)

    postQuery.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in
        if let objects = objects as? [PFObject] {

          // Remove content from arrays if it's the first query

            if error == nil {
                if arrayOfPosts.isEmpty == false {
                    for post in arrayOfPosts {

                     // Append stuff

                        if queryIsComplete == true {

                            if query == 0 {
                                self.loadStuff()

                            } else if query == 1 {

                                self.loadStuff()

                            } else {

                                self.arrays.appendContentsOf(self.arrayFromQuery1)
                                self.arrays.appendContentsOf(self.arrayFromQuery2)
                                self.arrays.appendContentsOf(self.arrayFromQuery3)

                                self.arrays.sortInPlace({ $0.createdAt!.compare($1.createdAt!) == NSComparisonResult.OrderedAscending })

                                self.table.reloadData()

                            }


                        }


                    }

                }

            }
        }
    })
}

2 个答案:

答案 0 :(得分:2)

我假设你异步获取大量记录,所以:

array.sortInPlace { $0.createdAt.compare($1.createdAt) == NSComparisonResult.OrderedAscending }

答案 1 :(得分:0)

如果你想知道在数组中创建每个对象的时间,你将在使用时间戳createAtupdateAt

从解析开始查询之前设置一个条件
 var timeStamp = NSDate(timeIntervalSinceNow: -1200)
 query.whereKey("createdAt", greaterThanOrEqualTo: timeStamp)

然后您可以像下面的

一样订购您的列表
   query.orderByDescending("createdAt")   //either Ascending or Descending

所以现在你将做我的朋友@DavidWong提议进入最高职位:)