问题
Hi i have the following method:
func getAlamoPlayers() ->[Player]{
//Get TeamID
let prefs:NSUserDefaults = NSUserDefaults.standardUserDefaults()
var TeamId:String = prefs.stringForKey("TEAMID")!
//create parameters for POST
let parameters =
["IdTeam": TeamId]
var result: NSArray?
//Request
Alamofire.request(.POST, "XXXXXX.php", parameters: parameters)
.responseJSON { (request, response, json, error) -> Void in
result = (json as NSArray)
// Make models from Json data
for (var i = 0; i < result?.count; i++) {
let dic: NSDictionary = result![i] as NSDictionary
//Create a team object
var p:Player = Player()
//Assign the values of each key value pair to the team object
p.PlayerID = dic["IdPlayer"] as String
p.PlayerName = dic["name"] as String
p.PlayerFkToTeam = dic["teams_IdTeam"] as String
//Add the team to the player array
self.players.append(p)
}
}
return self.players
}
So when i call this method in another class the players array is empty! How can I handle the asynchronous request of alamofire? I think the method returns the player object before the request is finished, or am I wrong?
Thanks in advance!
回答1:
Here is a question that asks basically the same thing you are.
AlamoFire GET api request not working as expected
So basically what is happening is that your post request is being called in a background queue. So think of it as calling your post then continuing on to the next line. So since it is in a background thread it doesn't set the variable till after your function returns.
You could use a completion handler as explained in detail in the link I provided. Another solution would be to use NSNotificationCenter. Set up an observer and then post a notification inside your POST request.
So for example: First declare your notification name at the top of the class where you will have your post response function as so. So that other classes can access it
let YOUR_NOTIFICATION_NAME = "NOTIFICATION NAME"
class YOUR_CLASS{
then inside the class write the code you want to execute once the POST is done.
func YOUR_FUNCTION{
...
}
Then before you execute your POST request you want to add an observer.
NSNotificationCenter.defaultCenter().addObserver(self, selector: "YOUR_FUNCTION", name: YOUR_NOTIFICATION_NAME, object: nil)
Then post your notification inside your POST request.
Alamofire.request(.POST, "XXXXXX.php", parameters: parameters)
.responseJSON { (request, response, json, error) -> Void in
...<your code>...
self.players.append(p)
NSNotificationCenter.defaultCenter().postNotificationName(YOUR_NOTIFICATION_NAME, object: self)
}
回答2:
so i've build it in the following way:
Modell Class with all requests:
let finishedPlayers = "finishedPlayers"
class StrafenkatalogModel: NSObject {
.
.
func getAlamoPlayers()-> [Player] {
//Get TeamID
let prefs:NSUserDefaults = NSUserDefaults.standardUserDefaults()
var TeamId:String = prefs.stringForKey("TEAMID")!
//create parameters for POST
let parameters =
["IdTeam": TeamId]
var result: NSArray?
//Observer
NSNotificationCenter.defaultCenter().addObserver(self, selector: "returnPlayers", name: finishedPlayers, object: nil)
//Request
Alamofire.request(.POST, "http://smodeonline.com/XXXXXXX.php", parameters: parameters)
.responseJSON { (request, response, json, error) -> Void in
result = (json as NSArray)
// Make models from Json data
for (var i = 0; i < result?.count; i++) {
let dic: NSDictionary = result![i] as NSDictionary
//Create a team object
var p:Player = Player()
//Assign the values of each key value pair to the team object
p.PlayerID = dic["IdPlayer"] as String
p.PlayerName = dic["name"] as String
p.PlayerFkToTeam = dic["teams_IdTeam"] as String
//Add the team to the teams array
self.players.append(p)
}
}
return players
}
func returnPlayers() ->[Player]{
return self.players
}
Then i want to call it in the TableViewController class (Class2):
class AddPlayersTableViewController: UITableViewController {
let model:StrafenkatalogModel = StrafenkatalogModel()
var players:[Player] = [Player]()
override func viewDidLoad() {
super.viewDidLoad()
// Get Player objects from the model
NSNotificationCenter.defaultCenter().postNotificationName("finishedPlayers", object: self)
self.players = self.model.returnPlayers()
.
.
}
But the players object is empty. Does you know why? Did i something wrong?
来源:https://stackoverflow.com/questions/29377824/swift-alamofire-return-value-is-empty