完成块处理空数组
问题描述:
我有一个类,我试图使用一些泛型代码为多个集合视图构建数组,并将数组数组传递给viewcontroller,其中集合视图是。完成块处理空数组
我刚才的问题是结果在查询完成之前传递给完成处理程序。
我在这里做错了什么?理论上,它应该工作我认为。
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)
。将其移入查询的完成块而不是之后。
您有:
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)
})
+0
好的调用,我还需要添加@在完成时转义:“completeBlock:@ escaping(_” – Pippo
或许,如果你缩进你的代码正确,你会看到,有接近尾声:) – Alexander
我不匹配括号没有看到没有不匹配的括号,但我认为你的'completeBlock(result:collectionArray)'应该在'})'之前(在名为'block'的块的末尾) – Toldy