我有4个包含Parse query.findObjectsInBackgroundWithBlock的函数。这些被调用以获取数据,然后填充表视图。使用调度组。

这是我的解析查询的两个示例

func getEventImages() {
    print("getEventImages enter")
    dispatch_group_enter(self.group)

    let query = PFQuery(className: "events")
    query.orderByAscending("eventDate")
    query.findObjectsInBackgroundWithBlock { (objects:[AnyObject]!, error: NSError!) -> Void in
        // Initialize your array to contain all nil objects as
        // placeholders for your images
        if error == nil {
            self.eventMainImageArray = [UIImage?](count: objects.count, repeatedValue: nil)
            for i in 0...objects.count - 1 {

                let object: AnyObject = objects[i]
                let mainImage = object["mainImage"] as! PFFile
                //dispatch_group_enter(self.group)
                mainImage.getDataInBackgroundWithBlock({
                    (imageData: NSData!, error: NSError!) -> Void in
                    if (error == nil) {
                            let mainImage = UIImage(data:imageData)
                            self.eventMainImageArray[i] = mainImage
                            print("getEventImages appended")
                    }
                    else {
                        print("error!!")
                    }
                })
            }
        }
        print("getEventImages leave")
        dispatch_group_leave(self.group)
    }
}

func getEventInfo() {
    print("eventInfo enter")
    dispatch_group_enter(group)

let query = PFQuery(className: "events")
    query.orderByAscending("eventDate")
    query.findObjectsInBackgroundWithBlock { (objects:[AnyObject]!,error:   NSError!) -> Void in
        self.eventNameArray = [String?](count: objects.count, repeatedValue: nil)
        self.eventInfoArray = [String?](count: objects.count, repeatedValue: nil)
        self.eventDateArray = [NSDate?](count: objects.count, repeatedValue: nil)
        self.eventTicketsArray = [String?](count: objects.count, repeatedValue: nil)

        if error == nil {
            for i in 0...objects.count - 1 {
                let object: AnyObject = objects[i]
                let eventName = object["eventName"] as! String
                let eventInfo = object["eventInfo"] as! String
                let eventDate = object["eventDate"] as! NSDate
                let eventTicket = object["Tickets"] as! String

                self.eventNameArray[i] = eventName
                self.eventInfoArray[i] = eventInfo
                self.eventDateArray[i] = eventDate
                self.eventTicketsArray[i] = eventTicket
                print("event info appended")
            }
        }
        print("event info leave")
        dispatch_group_leave(self.group)
    }
}


还有我的dispatch_group_nofity

 dispatch_group_notify(group, dispatch_get_main_queue()) { () -> Void in
        print("Finished reloadDataFromServer()")
        self.tableView.reloadData()
        self.refreshControl?.finishingLoading()
    }
}


问题是,如果在调用dispatch_group_leave(self.group)之前足够快地检索数据,则会造成数据丢失和丢失,从而导致过早重新加载tableview数据。我需要获取它,以便在追加完成时调用dispatch_group_leave。

最佳答案

不需要两种方法来检索数据,不需要将数据解压缩为多个数组,也不需要使用调度组。

您只需要一种简单的方法即可检索事件数据

var events:[PFObject]=[PFObject]()

func getEventInfo() {
    let query = PFQuery(className: "events")
    query.orderByAscending("eventDate")
    query.findObjectsInBackgroundWithBlock { (objects:[AnyObject]!,error:   NSError!) -> Void in
        if error==nil {
            self.events=objects as! [PFObject]
            self.tableView.reloadData()
        } else {
            print("Something went wrong! - \(error)"
        }
        self.refreshControl?.finishingLoading()
    }
}


然后,您没有显示cellForRowAtIndexPath,但是您会看到类似

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell = self.tableView.dequeueReusableCellWithIdentifier("cell") as! MyTableViewCell
    let event=self.events[indexPath.row]

    cell.eventName.text=event["eventName"] as? String
    cell.eventInfo.text=event["eventInfo"] as? String
    if let mainImageFile=event["mainImage"] as? PFFile {
        mainImageFile.getDataInBackgroundWithBlock({
            (imageData: NSData!, error: NSError!) -> Void in
            if (error == nil) {
                let mainImage = UIImage(data:imageData)
                cell.mainImage= mainImage
            }
            else {
                print("error!!")
            }
    }
    return cell;
}


您可以使用PFImageViewSDWebImage之类的框架来处理图像缓存,并在加载图像时将占位符图像放置在适当的位置。

如果您想更新事件,就像

 var event=self.events[someindex];
 event["eventName"]=newValue
 event.saveInBackground()

关于ios - 具有解析查询的Swift调度组,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34275028/

10-10 04:44