NSURLSession,完成块,Swift

时间:2015-10-16 03:44:07

标签: swift nsurlsession completion-block

我正在使用NSURLSession。我有一个餐厅的阵列,我要求阵列中的每个餐厅的菜肴到api。 dataTask工作,我只是在完成所有dataTasks时尝试调用方法真的很难。

 self.findAllDishesOfRestaurants(self.restaurantsNearMe) { (result) -> Void in
        if result.count != 0 {
              self.updateDataSourceAndReloadTableView(result, term: "protein")
        } else {
            print("not ready yet")
        } 
    }

无论我的完成块如何,都不会调用self.updateDataSourceAndREloadTableView。这是我的findAllDishesOfRestaurants功能

func findAllDishesOfRestaurants(restaurants:NSArray, completion:(result: NSArray) -> Void) {
    let allDishesArray:NSMutableArray = NSMutableArray()
    for restaurant in restaurants as! [Resturant] {
        let currentRestaurant:Resturant? = restaurant
        if currentRestaurant == nil {
            print("restaurant is nil")
        } else {
            self.getDishesByRestaurantName(restaurant, completion: { (result) -> Void in
                                            if let dishesArray:NSArray = result {
                                                restaurant.dishes =  dishesArray
                                                print(restaurant.dishes?.count)
                                                allDishesArray.addObjectsFromArray(dishesArray as [AnyObject])
                                                self.allDishes.addObjectsFromArray(dishesArray as [AnyObject])
                                                print(self.allDishes.count)
                                            }
                                            else {
                                                print("not dishes found")
                                        }
                                          // completion(result:allDishesArray)
                                    })
             completion(result:allDishesArray)
        }
    }
}

这是我执行dataTasks的功能。

 func getDishesByRestaurantName(restaurant:Resturant, completion:(result:NSArray) ->Void) {

    var restaurantNameFormatted = String()
    if let name = restaurant.name {
    for charachter in name.characters {
        var newString = String()
        var sameCharacter:Character!
        if charachter == " " {
           newString = "%20"
            restaurantNameFormatted = restaurantNameFormatted + newString
        } else {
            sameCharacter = charachter
            restaurantNameFormatted.append(sameCharacter)
        }
       // print(restaurantNameFormatted)
    }
}
    var urlString:String!
        //not to myself, when using string with format, we need to igone all  the % marks arent ours to replace with a string, otherwise they will be expecting to be replaced by a value
         urlString = String(format:"https://api.nutritionix.com/v1_1/search/%@?results=0%%3A20&cal_min=0&cal_max=50000&fields=*&appId=XXXXXXXXXappKey=XXXXXXXXXXXXXXXXXXXXXXXXXXXX",restaurantNameFormatted)
    let URL = NSURL(string:urlString)
    let restaurantDishesArray = NSMutableArray()
  let session = NSURLSession.sharedSession()
                let dataTask = session.dataTaskWithURL(URL!) { (data:NSData?, response:NSURLResponse?, error:NSError?) -> Void in
                do {
                let anyObjectFromResponse:AnyObject = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.AllowFragments)
                    if let asNSDictionary = anyObjectFromResponse as? NSDictionary {
                        let hitsArray = asNSDictionary.valueForKey("hits") as? [AnyObject]
                                                for newDictionary in hitsArray! as! [NSDictionary]{
                                                    let fieldsDictionary = newDictionary.valueForKey("fields") as? NSDictionary
                                                    let newDish = Dish.init(dictionary:fieldsDictionary!, restaurant: restaurant)
                                                    restaurantDishesArray.addObject(newDish)
                        }
                    }
                    completion(result:restaurantDishesArray)
                } catch let error as NSError {
                    print("failed to connec to api")
                    print(error.localizedDescription)
                }
            }
            dataTask.resume()
}

就像我之前说过的,我需要等到有趣的findAllDishesOfRestaurants完成。我试着写完我的完成块,但我不确定我做得对。任何帮助是极大的赞赏。感谢

1 个答案:

答案 0 :(得分:4)

问题是,在任务完成之前,您正在调用completion中的findAllDishesOfRestaurants方法。事实上,你为列表中的每个餐馆打电话一次,这可能不是你想要的。

我的建议是让您调查NSOperationQueue有两个原因:

  1. 它允许您限制对服务器的并发请求数,因此您的服务器不会充满请求。
  2. 它可以让您轻松控制所有操作的完成时间。
  3. 但是,如果您正在寻找快速解决方案,您需要的是使用GCD组dispatch_group_createdispatch_group_enterdispatch_group_leavedispatch_group_notify,如下所示。< / p>

    func findAllDishesOfRestaurants(restaurants:NSArray, completion:(result: NSArray) -> Void) {
        let group = dispatch_group_create() // Create GCD group
    
        let allDishesArray:NSMutableArray = NSMutableArray()
        for restaurant in restaurants as! [Resturant] {
            let currentRestaurant:Resturant? = restaurant
            if currentRestaurant == nil {
                print("restaurant is nil")
            } else {
                dispatch_group_enter(group) // Enter group for this restaurant
                self.getDishesByRestaurantName(restaurant, completion: { (result) -> Void in
                    if let dishesArray:NSArray = result {
                        restaurant.dishes =  dishesArray
                        print(restaurant.dishes?.count)
                        allDishesArray.addObjectsFromArray(dishesArray as [AnyObject])
                        // self.allDishes.addObjectsFromArray(dishesArray as [AnyObject])  <-- do not do this
                        // print(self.allDishes.count)
                    }
                    else {
                        print("not dishes found")
                    }
                    // completion(result:allDishesArray)  <-- No need for this, remove
                    dispatch_group_leave(group) // Leave group, marking this restaurant as complete
                })
                // completion(result:allDishesArray) <-- Do not call here either
            }
        }
    
        // Wait for all groups to complete
        dispatch_group_notify(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0)) {
            completion(result:allDishesArray)
        }
    }