responseObject build error
Closed this issue · 7 comments
Hi,
I got this error when build this class :
Cannot convert value of type '(Response<InventoryCategory, NSError>) -> ()' to expected argument type 'Response<_, NSError> -> Void'
import Foundation
import Alamofire
import ObjectMapper
import AlamofireObjectMapper
import RealmSwift
class ProductCategorySync {
static func get (){
Alamofire.request(ProductCategory.ReadAll())
.responseObject { (response: Response<InventoryCategory, NSError>) in
switch response.result {
case .Success:
let items = response.result.value
if let items = items?.result {
let realm = try! Realm()
try! realm.write {
for item in items {
realm.add(item, update: true)
}
}
}
case .Failure:
break
}
}
}
}
Here the class for Alamofire request :
import UIKit
import Alamofire
enum ProductCategory: URLRequestConvertible {
static let baseURLString = ApiConfig.baseURL
case ReadAll()
var method: Alamofire.Method {
switch self {
case .ReadAll:
return .POST
}
}
// MARK: URLRequestConvertible
var URLRequest: NSMutableURLRequest {
ApiConfig.model = ApiConfig.MODEL_PRODUCT_CATEGORY
let URL:NSURL! = NSURL(string: ProductCategory.baseURLString)
let mutableURLRequest = NSMutableURLRequest(URL: URL)
mutableURLRequest.HTTPMethod = method.rawValue
var param_detail : Array<Dictionary<String, AnyObject>> = Array()
switch self {
case .ReadAll():
ApiConfig.method = ApiConfig.METHOD_SEARCH_READ
param_detail.append(["opts" : ApiConfig.opts, "moduleDetail" : ApiConfig.moduleDetailArr])
let parameters = ["jsonrpc": "2.0", "method": "api", "params": param_detail, "id" : 1]
return Alamofire.ParameterEncoding.JSON.encode(mutableURLRequest, parameters: (parameters as! [String : AnyObject])).0
default:
return mutableURLRequest
}
}
}
The code worked fine for parsing JSON data, but got error when I added lines for mapping JSON to Realm.
Thank you and appreciate for any help ...
I had this issue as well. It was because my class to map to was not extending Mappable.
import Foundation
import RealmSwift
import ObjectMapper
class InventoryCategory: Object, Mappable { // <-- you are probably missing `Mappable`
// Realm and ObjectMapper code here
}
@levibostian I have extended my class to Mappable as :
import RealmSwift
import ObjectMapper
class InventoryCategory: Object, Mappable
{
dynamic var id : Int = 0
dynamic var name : String = ""
dynamic var type : String = ""
dynamic var created_at = NSDate()
dynamic var modified_at = NSDate()
....
}
But the error still persists
Hmmmm...I don't have another idea for you at the moment. I will have to keep playing around with my project and come back if I have any more ideas for you.
I found the solution, changed method to responseJSON and added NSJSONSerialization lines to retrieve the response. It worked, and I still didn't know why using method responseObject produced above error.
import Foundation
import Alamofire
import ObjectMapper
import RealmSwift
import SwiftyJSON
class ProductCategorySync {
static func loadData()-> Void{
Alamofire.request(ProductCategory.ReadAll())
.responseJSON { response in
switch response.result {
case .Success:
let realm = try! Realm()
if let json = response.result.value {
let item = JSON(json)
let results : NSData = try! item["result"].rawData()
do{
if let jsonResult = try NSJSONSerialization.JSONObjectWithData(results, options: []) as? NSArray {
for dict in jsonResult as! [NSDictionary] {
let category = InventoryCategory.mappedCategory(dict as! Dictionary<String, AnyObject>)
try realm.write{
realm.add(category, update: true)
}
}
}
} catch let error as NSError {
print(error)
}
}
break
case .Failure:
break
}
}
}
}
And added this method to InventoryCategory Model class :
static func mappedCategory(dict:Dictionary<String, AnyObject>) -> InventoryCategory{
return Mapper<InventoryCategory>().map(dict)! as InventoryCategory
}
Thank you!
Hmmm...not quite the solution you were looking for as .responseJSON
is an Alamofire function not a AlamofireObjectMapper function.
From viewing your code I didn't see any issues. Not quite sure how to help without seeing more. Glad you have something working though.
I can share some code that is working for myself too.
Yea I know .. but this is the fastest solution to make my code works, actually I want to use .responseObject but time is running to finish the project. If you don't mind you can share your working code here, Thank you.
Hmmm...not quite the solution you were looking for as
.responseJSON
is an Alamofire function not a AlamofireObjectMapper function.From viewing your code I didn't see any issues. Not quite sure how to help without seeing more. Glad you have something working though.
I can share some code that is working for myself too.
Yes, please! Can you please show us your code?