1

只有PFObject当我在单独的函数中从我的 Parse 后端检索到某个函数时,我才尝试只调用一个函数。目前我在设置延迟后调用第二个函数3.0并且它正在工作,但前提是第一个查询函数在 内调用3.0,否则我必须pullToRefreshtableView第一个函数最终完成后才能填充 Parse 数据. (顺便说一句,我正在使用PFQueryTableViewController

这是我目前的代码(我知道queryForTable这是一个覆盖,所以无论如何都会被调用,所以有没有办法将其更改为普通函数?):

override func viewDidLoad() {

    // ideally want something like "if getX'IsComplete' then queryForTable() and self.loadObjects()"

    retrieveFirstX()

    let delay = 3.0 * Double(NSEC_PER_SEC) // retrieveFirstX must load within 3.0 for table to populate without needing to pullToRefresh
    let time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))
    dispatch_after(time, dispatch_get_main_queue(), {

        self.queryForTable()
        self.loadObjects()
    })
}

var X = PFObject(className: "X")

func retrieveFirstX() -> Bool {

    let xQuery = PFQuery(className: "X")
    xQuery.orderByDescending("createdAt")
    xQuery.getFirstObjectInBackgroundWithBlock {
        (object: PFObject?, error: NSError?) -> Void in
        if error == nil {

            self.X = object!
        }
    }
    return true
}

 override func queryForTable() -> PFQuery {

    let xRelation = X.relationForKey("xPosts") as PFRelation!
    let relationQuery = xRelation.query()

    let xPostsQuery = PFQuery(className: "Posts")
    xPostsQuery.includeKey("postUser")
    xPostsQuery.whereKey("objectId", matchesKey: "objectId", inQuery: relationQuery)
    xPostsQuery.cachePolicy = .NetworkElseCache
    xPostsQuery.orderByDescending("createdAt")
    return xPostsQuery
}

我是否需要使用完成处理程序,如果需要,我该怎么做,因为我以前从未使用过它们?

提前致谢!

4

1 回答 1

3

完成处理程序听起来不错,例如:

override func viewDidLoad() {
    let completionHandler = {
        self.queryForTable()
        self.loadObjects()
    }
    retrieveFirstX(completionHandler)
}

func retrieveFirstX(completion: ()->()) -> Bool {
    let xQuery = PFQuery(className: "X")
    xQuery.orderByDescending("createdAt")
    xQuery.getFirstObjectInBackgroundWithBlock {
        (object: PFObject?, error: NSError?) -> Void in
        if error == nil {
            self.X = object!
            completion()
        }
    }
    return true
}
于 2015-12-13T16:05:11.660 回答