在Parse中从指针数组中检索对象的更有效方法,然后在Swift

时间:2015-09-24 16:20:32

标签: arrays swift uitableview pointers parse-platform

我已将一个Parse“Event”对象传递给viewController。无需查询。

这个“Event”对象包含一个指向Parse“Comment”对象的指针数组。

我对“评论”对象的两个属性感兴趣。两者都是名为“commentAuthor”和“commentText”的字符串。

我有一个UITableView,它有原型单元格,需要显示“commentAuthor”和“commentText”字符串。

它目前正在运作,但我对我这样做的方式并不满意,这就是:

var commentsArrayOfPointers: [AnyObject]?
var commentsArrayOfObjects = [PFObject]()

func dealWithCommentPointers() {

    //get array of pointers from "Event" object
    commentsArrayOfPointers = theEvent!["commentsArray"] as? [AnyObject]

    commentsArrayOfObjects.removeAll()

    //loop through each pointer, and "Fetch" its associated "Comment" object,
    //then append that "Comment" object to my array of "Comment" objects
    for comment in commentsArrayOfPointers! {

        comment.fetchIfNeeded()

        commentsArrayOfObjects.append(comment as! PFObject)

    }

    //print(commentsArrayOfObjects)

    self.tableView.reloadData()

}

然后,我像这样构建我的表格单元格,从我之前填充的“commentsArrayOfObjects”中提取数据:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("commentCell",
      forIndexPath: indexPath) as! commentCellClass

    cell.commentCellNameLabel.text = commentsArrayOfObjects[indexPath.row]["commentAuthor"] as! String
    cell.commentCellCommentLabel.text = commentsArrayOfObjects[indexPath.row]["commentText"] as! String

    return cell
}

它可以工作,但我对在每个评论对象上使用Parse命令“fetchIfNeeded()”并不感到激动,因为它是一个同步函数,这意味着它会导致我的应用在显示表之前暂停。我更喜欢使用一个异步函数,它允许加载视图控制器的其余部分,然后注释将在表可用时填充表。

我尝试做一个fetchInBackgroundIfNeeded()这是一个异步命令,但当然它在构建原型单元之前没有完成,导致崩溃(因为它正在寻找数据的数组是nil)。

我也尝试在我需要的“事件”上设置PFQuery(事件虽然我已经有事件,不需要重新查询它)并在“事件”的“commentsArray”属性中添加“includeKey” “上课。然后在查询结束时,我做了self.tableView.reloadData(),但这也没有用。这里似乎存在一个更大的问题,因为查询似乎从未执行过,并且表重新加载,即使我已经注释掉了代码来执行此操作。这是尝试(我认为更接近我的问题的正确解决方案):

func dealWithCommentPointers() {

    print("inside the function")

    var query = PFQuery(className: "Event")
    let objectIdToLookup = self.theEvent?.objectId
    query.whereKey("objectId", equalTo: objectIdToLookup!)
    query.includeKey("commentsArray")

    query.findObjectsInBackgroundWithBlock { (results, error) -> Void in

        if error != nil {
            print("Error found")
            print(error)
        }


        else {

            let eventArray = results as! [PFObject]

            let theEventToDisplayCommentsFor = eventArray[0] //this should always be [0]

            print("yo dude")

            self.commentsArrayOfObjects = theEventToDisplayCommentsFor["commentArray"] as! [PFObject]

            print("hey here i am!")

            print(self.commentsArrayOfObjects.count)

            self.tableView.reloadData() //even tried commenting this out, it is still called?!

        }

    }

}

那么,有什么想法吗?

2 个答案:

答案 0 :(得分:2)

为什么不预先在数组中包含所有Comment个对象?我的猜测是你在前面的视图控制器上执行了一个查询,这就是你传递Event对象的方式。当你执行那个查询时,在数组上使用includeKey所以所有的Comment对象与查询同时返回。

如果无法完成,我建议在表视图中添加一个函数,以异步方式从Parse中获取数据,然后重新加载表。 Here's an example for doing that which I've posted before。在那个例子中有一个UISearchController,但想法是一样的。

答案 1 :(得分:1)

您应该使用注释的关系而不是数组,然后您有一个可以异步运行以获取注释的查询。此外,您需要更改表视图设置,以便在您还没有任何注释时不会崩溃。你没有说明崩溃的原因,所以我无法解决这个问题,但你应该在查询正在进行时确实有一个活动指示器,然后将其替换为真实单元格或表示尚无评论的单元格。< / p>

从技术上讲,使用异步提取对于少量评论应该也可以正常工作,但是当你添加更多评论时,你会发出更多的网络请求,而且你很快就会涌入网络,它们都会失败。