-1

I am attempting to assign the data from my Alamofire request to a dictionary of parameters, but it always shows up as nil.

var params = SomeParams()
params.addUserIds([currentUserID, partnerUserID])
params.name = name

Alamofire.request(url!, method: .get)
            .validate()
            .responseData(completionHandler: { (responseData) in
             DispatchQueue.main.async {                                              
                 params.coverImage = responseData.data!  
             }                                                       
})

// NIL RESPONSE
print(params)

I know that it's because the Alamofire call is being done in the background so I attempted to call the main thread and assign it. Yet it still doesn't work. Any ideas?

butter_baby
  • 808
  • 1
  • 9
  • 23

1 Answers1

-1

You need to perform work (like print) in the completionHandler. There is no need of DispatchQueue

var params = SomeParams()
params.addUserIds([currentUserID, partnerUserID])
params.name = name

Alamofire.request(url!, method: .get)
            .validate()
            .responseData(completionHandler: { (responseData) in

                 params.coverImage = responseData.data!  
                 print(params)

})
pkc456
  • 8,256
  • 34
  • 50
  • 104
  • Actually there is a need for the dispatchqueue, his example above isn't actually setting an image but if he was setting the `UIImage` he will need to do it on the main queue. He may also want the params outside of the request completion so this doesn't resolve that problem – Bot Feb 13 '19 at 18:21
  • @Bot: I thought `params` is a model class where `coverImage` is a property name. – pkc456 Feb 13 '19 at 18:22