迅速关闭问题

时间:2016-07-06 20:11:33

标签: swift firebase closures firebase-realtime-database

我只是在Swift中进行攻击,但对于我的生活,除了空return之外,我无法将其归结为[Item]。如果我在闭包内执行[Item]函数,print是正确的以及我想要它的方式。

func getCurrentDetails(ref: FIRDatabaseReference) -> [Item] {
    ref.observeEventType(.Value, withBlock: { snapshot in
        var currentDetails = [Item]()
        for item in snapshot.children {
            var currentItem = Item(snapshot: item as! FIRDataSnapshot)
            if currentItem.Name != "" { 
                currentItem.Image = "http://www.deletedthisurl/" + currentItem.Number + "_500X500.gif"
                currentDetails.append(currentItem)   
            }   
        }
        self.currentArray = currentDetails
        // print(self.currentArray)  // Prints here
    })
    print(self.currentArray)  //Prints an empty Item array here
    return currentArray
}

1 个答案:

答案 0 :(得分:0)

由于对firebase的调用是assync,你将无法返回该值。

要处理它,您可以使用完成处理程序。

SELECT DISTINCT `wordpress`.p.ID as wordpress_post_id, `wordpress`.t.name as wordpress_author_name, n.nid as drupal_id, au.title as drupal_author_name
FROM `wordpress`.wp_posts p
INNER JOIN `wordpress`.wp_term_relationships r ON r.object_id = p.ID
INNER JOIN `wordpress`.wp_term_taxonomy tax ON tax.term_taxonomy_id = r.term_taxonomy_id
INNER JOIN `wordpress`.wp_terms t ON t.term_id = tax.term_id
LEFT OUTER JOIN `drupal`.node n ON p.ID = n.nid
LEFT OUTER JOIN `drupal`.content_field_op_author a ON n.nid = a.nid
LEFT OUTER JOIN `drupal`.node au ON t.name = au.title
WHERE tax.taxonomy = 'author'
AND `wordpress`.t.name != au.title
;

致电时,您应该执行以下操作:

func getCurrentDetails(ref: FIRDatabaseReference, completion:(currentArray:[Item])->()) {
    ref.observeEventType(.Value, withBlock: { snapshot in
        var currentDetails = [Item]()
        for item in snapshot.children {
            var currentItem = Item(snapshot: item as! FIRDataSnapshot)
            if currentItem.Name != "" { 
                currentItem.Image = "http://www.deletedthisurl/" +
                    currentItem.Number + "_500X500.gif"
                currentDetails.append(currentItem)   
            }   
        }
        self.currentArray = currentDetails
        completion(currentArray)
    })
}
相关问题