完成块处理空数组

Posted

技术标签:

【中文标题】完成块处理空数组【英文标题】:Completion Block handing empty array 【发布时间】:2017-01-02 21:21:16 【问题描述】:

我有一个类,我在其中尝试使用一些通用代码为多个集合视图构建数组并将数据数组传递给集合视图所在的视图控制器。

我刚才遇到的问题是,在查询完成之前,结果正在传递给完成处理程序。

我在这里做错了什么?我认为理论上它应该有效。

import UIKit
import Parse

class BuildArray: UICollectionViewController 
    class func buildArrayFromQuery(queryForCollection: String, sender: UIViewController, completeBlock: (_ result: Any) -> Void) 
        struct collectionStruct 
            var name : String
            var description : String
            var title : String
            var image : PFFile
            var id: String
        

        var collectionArray = [collectionStruct]()

        //var queryFromSource =
        var fromClass = ""
        var fromName = ""
        var fromDescription = ""
        var fromTitle = ""
        var fromImage = ""

        //------------------------------------------------------------- set the class to query based off the collection

        if queryForCollection == "Pack" 
            fromClass = "Pack"
            fromName = "packName"
            fromDescription = "packDescription"
            fromTitle = "packTitle"
            fromImage = "packImage"
         else if queryForCollection == "Part" 
            fromClass = "Part"
            fromName = "partName"
            fromDescription = "partDescription"
            fromTitle = "partTitle"
            fromImage = "partImage"
        

        //------------------------------------------------------------- check reachability and set LDS if required

        var query = PFQuery(className: fromClass)
        query.order(byAscending: fromName)

        CheckReachability.setupReachability(hostName: nil)  (block) in
            if (block) 
                //var connected = true
             else 
                //var connected = false
                query.fromLocalDatastore()
            
        

        //------------------------------------------------------------- find the objects

        query.findObjectsInBackground(block:  (objects, error) in
            if error != nil 
                print(error!)
             else if let packs = objects 
                //------------------------------------------------------------- if we have objects and were reachable unpin everthing so it can be updated
                CheckReachability.setupReachability(hostName: nil)  (block) in
                    if (block) 
                        PFObject.unpinAllObjectsInBackground(withName: "packDataLDS")
                    
                

                //------------------------------------------------------------- if there are objects loop through

                for object in packs 
                    print(fromName)
                    print(object)

                    let arrayName = object.object(forKey: fromName) as! String
                    let arrayDescription = object.object(forKey: fromDescription) as! String
                    let arrayTitle = object.object(forKey: fromTitle) as! String
                    let arrayImage = object.object(forKey: fromImage) as! PFFile
                    let arrayID = object.objectId as String!

                    collectionArray.append(collectionStruct(name: arrayName, description: arrayDescription, title: arrayTitle, image: arrayImage, id: arrayID!))
                

                //                DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) 
                //                    sender.collectionView?.reloadData()
                //                    sender.collectionView?.layoutIfNeeded()
                //                    sender.collectionView?.setScaledDesginParam(scaledPattern: .verticalCenter, maxScale: 1.0, minScale: 0.8, maxAlpha: 1.0, minAlpha: 0.5)
                //                    sender.collectionView?.scaledVisibleCells()
                //
                ////                    if sender.collectionView?.dataSource?.collectionView(sender.collectionView!, cellForItemAt: IndexPath(item: 0, section: 0)) != nil 
                ////                        //self.collectionView?.scrollToItem(at: IndexPath(item: self.packIndexLDS, section: 0), at: .centeredVertically, animated: true)
                ////                        let rect = sender.collectionView.layoutAttributesForItem(at: IndexPath(item: sender.packIndexLDS, section: 0))?.frame
                ////                        self.collectionView.scrollRectToVisible(rect!, animated: false)
                ////                    
                //                
            

            //------------------------------------------------------------- if we are reachable pin everthing

            CheckReachability.setupReachability(hostName: nil)  (block) in
                if (block) 
                    PFObject.pinAll(inBackground: objects, withName: "packDataLDS")
                
            

        )

        completeBlock(result: collectionArray)
    

【问题讨论】:

也许如果你正确缩进你的代码,你会看到结尾附近有不匹配的括号:) 我没有看到不匹配的括号,但我认为你的 completeBlock(result: collectionArray) 应该在 ) 之前向上(在名为 block 的块的末尾) 【参考方案1】:

您在查询的完成块之外调用completeBlock(result: collectionArray)。将其移动到查询的完成块内而不是之后。

你有:

    query.findObjectsInBackground(block:  (objects, error) in
        // lots of stuff removed for clarity
    )

    completeBlock(result: collectionArray)

但它必须是:

    query.findObjectsInBackground(block:  (objects, error) in
        // lots of stuff removed for clarity

        completeBlock(result: collectionArray)
    )

【讨论】:

好调用,我还需要在完成中添加@转义:“completeBlock:@转义(_”

以上是关于完成块处理空数组的主要内容,如果未能解决你的问题,请参考以下文章

Swift 4 完成处理程序:数组保持为空

完成处理程序在运行另一个任务之前完成任务

完成处理程序和块之间的区别:[iOS]

iOS-空数组的处理

从对象处理空的Json数组

未调用 NSURLSessionDataTask 完成处理程序块