Swift/Parse: 如何获取最近填写的项目?

Swift/Parse: how to get most recent item that has data filled in?

好的,我在任何地方都找不到这个。在 Swift 中,我需要获取最近添加的 Parse 项目,该项目有 2 个特定列填充了非 nil 数据。如果最近的 1 个或多个列为零,我需要查询 "select" 并从下一个包含所有数据的最新项目中获取数据。

这就是我尝试过的方式 - 我可以使用 .order(byDescending: "createdAt") 成功获取最新的项目,但由于我需要获取字符串和图像,我无法将其设为 select 最近的项目已填写所有数据。

我试图遍历对象数组和 select a PFObject 但这造成了错误,因为我不想使 VC class a PFSubclassing class.

//Parse - store global values
            var query = PFQuery(className: PARSE_CLASS!)
            query.order(byDescending: "createdAt")
            query.limit = 1
            query.findObjectsInBackground {
                (objects, error) -> Void in

                var objToUse = PFObject()

                if error == nil {
                    //get obj
                    for o in objects!
                    {
                        if(o["testTxt"] != nil && o["testImg"] != nil)
                        {
                            objToUse = o
                        }
                    }

                    //--------------------------------------------------->
                    for item in contentLinkKeys
                    {

    //                txtSources[item] = (objects?.first?["\(item)-blurb"] as! String)
                        if(objToUse["testTxt"] != nil && objToUse["testImg"] != nil)
                        {

                            txtSources[item] = ((objToUse["testTxt"] as! String) as! String)

                        if let validObjects = objects {
                            for object in validObjects {
                                let thumbnail = objToUse["testImg"] as? PFFile
                                thumbnail?.getDataInBackground (block: { (data, error) -> Void in
                                    //read image here
                                    var img = UIImage(data: data!)
                                    imgSources[item] = img

                                })
                            }
                            }


                        } //if }

                    } //for bracket
                }

我该怎么做?现在,如果最近的一张没有图片或其他任何图片,它就不会显示图片。我怎样才能让它知道去迭代并找到下一个确实填充了所有列的最新信息?

您可以执行以下操作。这里发生的是,我们首先以创建日期顺序获取所有数据,然后过滤掉那些有效的数据,即那些具有所有必填字段的数据。然后我们下载图像数据并将其添加到源项目。

class ParseObject: PFObject, PFSubclassing {

    @NSManaged var testTxt: String?
    @NSManaged var testImage: PFFile?

    class func parseClassName() -> String {
        return "ParseClassName"
    }
}


func test() {

    var itemSource = [UIImage]()

    let query = PFQuery(className: "ParseClassName")
    query.order(byDescending: "createdAt")
    //query.limit = 1 why 1 it will always fetch one object we have to iterate so let gets some more
    query.findObjectsInBackground { (objects, error) in
        if let error = error {
            //error occured
            print(error.localizedDescription)
        }
        else {
            //now we have the objects so we filter the once that doesn't have nil data for those fields
            if let objects = objects as? [ParseObject] {
                let filteredObjects = objects.filter{ ([=10=].testTxt != nil && [=10=].testImage != nil)}

                /////// If you want all the objects that are valid i.e tesTxt and testImage are certainly in there //////////

                for parseObject in filteredObjects {
                    let thumbnail = parseObject.testImage!
                    thumbnail.getDataInBackground(block: { (data, error) in
                        if let error = error {
                            //error
                            print(error.localizedDescription)
                        }
                        else {
                            if let image = UIImage(data: data!) {
                                //set the image
                                itemSource.append(image)
                            }
                        }
                    })
                }
            }
        }

    }

}

但是不直接在集合中追加或添加图像并不是一个好主意。您可以使用对象集合并在需要时下载图像