i'm pretty new in iOS and want and using of Alamofire. I want ask following: how to call some completion block when all items (in my case users) is already handled. Completion block is in defer block

**StopsHandler.swift**

```
func requestStopsForUser(user: User, completion: (result: RequestResult, json: JSON?) -> Void) {
    alamofireManager?.request(.GET, "\(AppSettings.ApiURL)/v1/users/\(user.id)/stops.json", headers: ["Authorization": "Token token=\(user.apiKey)"]).responseJSON { response in
        switch response.result {
        case .Success:
            if let value = response.result.value {
                completion(result: .Success, json: JSON(value))
            }
        case .Failure(let error):
            if error.code == NSURLErrorTimedOut {
                completion(result: .TimedOut, json: nil)
            } else {
                completion(result: .ConnectionFailed, json: nil)
            }
        }
    }
}

```

**Main.swift**

```
func fetchUsersAndStops(completion: (result: RequestResult) -> Void ) {

    var allStopsToWrite = [[Stop]]() //for each user we have array of stops to write in model

    requestAllUsers() { result, json in
        switch result {
        case .Success:
            let users = self.usersFromJSON(json)

            for (i, user) in users.enumerate() {   
                StopsHandler.sharedInstance.requestStopsForUser(user) { result, json in
                    print("i in = \(i)")
                    switch result {
                    case .Success:
                        defer {
                            let isLastUser = (i == users.count - 1)
                            if isLastUser {
                                try! self.realm.write(transactionBlock: {
                                    for (index, stopsToWrite) in allStopsToWrite.enumerate() {
                                        users[index].stops.appendContentsOf(stopsToWrite)
                                        self.realm.add(users[index], update: true)
                                    }},
                                    completion: {
                                        completion(result: .Success) //I want to call this completion when last user is already handled.
                                })
                            }
                        }

                        guard let json = json else {return}
                        let stops = StopsHandler.sharedInstance.stopsFromJSON(json)

                        let globalStops = self.realm.objects(Stop)

                        var stopsToWrite = [Stop]()

                        for stop in stops {
                            if globalStops.filter("id = '\(stop.id)'").first == nil {
                                stopsToWrite.append(stop)
                                if let currentUserId = self.currentUser?.id {
                                    if currentUserId == user.id {
                                        user.loggedIn = true
                                    }
                                }
                            }
                        }
                        allStopsToWrite.append(stopsToWrite)
                    case .TimedOut:
                        completion(result: .TimedOut)
                    case .ConnectionFailed:
                        completion(result: .ConnectionFailed)
                    }
                }
            }
        case .TimedOut:
            completion(result: .TimedOut)
        case .ConnectionFailed:
            completion(result: .ConnectionFailed)
        }
    }
}

```

I assume that my code runs in `main_queue` and i want the output the following (i have 4 users on server):

```
i in = 0
i in = 1
i in = 2
i in = 3

```

But suddenly i have the following: 

```
i in = 3
i in = 1
i in = 2
i in = 0

```

And I don't know why. Any help is massively appreciated! Thanks in advance!