从for循环中的第一个项目中检索数据

时间:2017-01-08 02:00:00

标签: ios arrays swift loops for-loop

我有一个查询从服务器获取对象然后通过将“packName”与“className”匹配来减少对象的数量,这应该只给我“packName”的子项。

从这里我填充一个结构项数组并拉出数组的第一个索引的数据。

这很好,但我有点担心,如果孩子的数量增加,这可能会减慢处理速度。所以我想知道是否有办法只检索for循环的第一项,这是我所追求的,因为查询已按升序排序。

这是下面的功能代码。

class func createHistory(packName: String, completeBlock: ((Bool) -> Void)? = nil) {

    struct initialDataStruct {
        var packNameStruct : String
        var packIdStruct : String
        var partNameStruct : String
        var partIdStruct : String
        var partIndexStruct : Int
    }

    var initialDataArray = [initialDataStruct]()

    let historyClass = PFObject(className: packName)

    let query = PFQuery(className: "Part")
        query.includeKey("fromPack")
        query.order(byAscending: "partName")
        query.fromLocalDatastore()

        query.findObjectsInBackground { (objects, error) in

        if error != nil {
            print(error!)
        }

        else if let parts = objects {

            for object in parts {

                // if the fromPack column has data
                if let fromPack = object.object(forKey: "fromPack") as? PFObject {

                    // create the class name from the pack name
                    if let className = (fromPack.object(forKey: "packName") as? String) {

                        // packName was sent from JVC
                        // this will limit array items to how ever many children packName has
                        if packName == className {

                            // because its sorted could probably just get the first item here
                            let packName = fromPack.object(forKey: "packName") as! String
                            let packId = fromPack.objectId as String!
                            let partName = object.object(forKey: "partName") as! String
                            let partId = object.objectId as String!
                            let partIndex = 0

                                initialDataArray.append(initialDataStruct(packNameStruct: packName,
                                                                            packIdStruct: packId!,
                                                                                partNameStruct: partName,
                                                                                    partIdStruct: partId!,
                                                                                        partIndexStruct: partIndex))

                        }
                    }
                }
            } // for 

            historyClass.add(initialDataArray[0].packNameStruct, forKey: "packName")
            historyClass.add(initialDataArray[0].partIdStruct, forKey: "packId")
            historyClass.add(initialDataArray[0].partNameStruct, forKey: "partName")
            historyClass.add(initialDataArray[0].partIndexStruct, forKey: "partIndex")

            print(historyClass)

            PFObject.pinAll(inBackground: [historyClass])

        }
    } // query
}

0 个答案:

没有答案