调用Parse getDataInBackground后,函数无法完成

时间:2016-03-16 21:26:13

标签: ios swift asynchronous parse-platform

我试图从解析中收集数据以创建一个Post对象数组,然后填充表视图。我想以这样一种方式创建Post对象:一旦它被初始化,它就不必依赖Parse服务器在其Table View单元格出列并重新排队时获取其数据。但是,在初始化post对象时,它永远不会超过updataPostImageAndUsername行,然后将线程引回到loadTimelinedata,然后它永远不会到达self.timeline.append(post)。我尝试使用completionHandlers,一旦完成异步数据检索就会调用它来修复问题,但似乎没有任何工作。

这是Post类

class Post: NSObject {
var data:Dictionary<String,AnyObject>!
var post:PFObject!

init(post: PFObject){
    super.init()
    self.post = post
    data = Dictionary<String,AnyObject>()
    data["content"] = post["content"] as! String
    self.updatePostImageAndUsername({
        (imageData: NSData) in
        self.data["image"] = UIImage(data: imageData)
    })
    self.updatePostDate()

}

private func updatePostImageAndUsername(completionHandler: (imageData:NSData) -> ()){
    let user = post["user"] as! PFUser
    data["postee"] = user.username!
    let imageFile = post["profileImage"] as! PFFile
    imageFile.getDataInBackgroundWithBlock({
        (imageData:NSData?, error: NSError?) in
        if let imageData = imageData {
            completionHandler(imageData: imageData)
        }
    })
}

private func updatePostDate() {
    let dateFormatter = NSDateFormatter()
    dateFormatter.dateStyle = NSDateFormatterStyle.ShortStyle
    dateFormatter.timeStyle = .ShortStyle
    data["createdAt"] = dateFormatter.stringFromDate(post.createdAt!)
}

}

这就是我创建帖子的地方

class TimelineTableViewController: UITableViewController {
var timelineData = [Post]()

/////////////////
///Data Functions
/////////////////

func loadTimelinedata(completionHandler: (post: PFObject) -> ()) {
    self.timelineData.removeAll()
    let posts = PFQuery(className: "Posts")
        posts.findObjectsInBackgroundWithBlock({
        (objects: [PFObject]?, error: NSError?) -> Void in
        if let objects = objects {
            for object in objects {
                completionHandler(post: object)
            }
        }
    })

}

override func viewDidAppear(animated: Bool) {
    loadTimelinedata({
        (post: PFObject) in
        let post = Post(post: post)
        self.timelineData.append(post)
    })
}

所以基本上我的问题是为什么这些函数在getDataInBackground完成后退出?

0 个答案:

没有答案