Reputation: 878
Hello
I'm trying to use Alamofire library to access to a Rest API, The problem that I'm having is the following:
I have the following method:
func LoadDevices() ->Void{
dispatch_async(dispatch_get_global_queue(priority, 0)) {
// do some task
do{
try self.devices = self.manager.GetDevices(self.identity?.IdentityUser, password: self.identity?.IdentityPassword)!
let lastDeviceConnected = self.manager.RetrieveSharedLastDeviceConnected()
if(self.devices.count > 0 && lastDeviceConnected == nil){
self.manager.SaveSharedLastDeviceConnected(self.devices[0].DeviceNumber!)
self.manager.SaveSharedLastDeviceConnectedObject(self.devices[0])
self.deviceData = try self.manager.GetDeviceData(self.identity?.IdentityUser!, password: self.identity?.IdentityPassword!, deviceNumber: self.devices[0].DeviceNumber!)
if(self.deviceData != nil){
self.manager.SaveSharedDeviceDataJson(DeviceData.DeviceDataToJson(self.deviceData!))
}
}
}catch{
self.devices = [Device]()
}
dispatch_async(dispatch_get_main_queue()) {
self.tableView.reloadData()
}
}
}
In this method I'm calling a method "GetDevices", this method have the following code:
func GetDevices(user: String?, password: String?) ->[Device]?{
let myIdentity = RetrieveSharedIdentityObject()
let url = "https://" + (myIdentity?.ServerAccount)! + "/account/account/account/" + (myIdentity?.IdentityAccountNumber)! + "/devices"
var devices : [Device]?
let headers = [
"MMSSession": myIdentity!.ServerAccountToken!,
]
Alamofire.request(.GET, url, headers: headers)
.responseJSON { response in
print(response.data)
let json = JSON(data: response.data!)
print(json)
let device = Device()
devices?.append(device)
}
return devices
}
As you can see I'm trying to access to an API using Alamofire, but when I executed this I don't get the data printed and also I'm getting an exception because i'm getting a nil value in the first method.
Please any help with this probably someone have any idea of why the Alamofire code doesn't work.
Upvotes: 0
Views: 166
Reputation: 17710
The callback which received the data is called asynchronously. So you need to update the date in the callback, not after the call (which will return immediately, before having received the data).
Upvotes: 2