evermeer / EVReflection

Reflection based (Dictionary, CKRecord, NSManagedObject, Realm, JSON and XML) object mapping with extensions for Alamofire and Moya with RxSwift or ReactiveSwift
Other
966 stars 119 forks source link

Property mapping is not called #266

Open amgadserry opened 6 years ago

amgadserry commented 6 years ago

public class Address: EVManagedObject {

    override public func propertyMapping() -> [(keyInObject: String?, keyInResource: String?)] {
        return [
            (keyInObject: "firstName",keyInResource: "first_name"),
            (keyInObject: "lastName",keyInResource: "last_name"),
            (keyInObject: "address1",keyInResource: "address_1"),
            (keyInObject: "address2",keyInResource: "address_2")
        ]
    }

    func updateRemote(onSuccess: @escaping ()->(), onFailure: @escaping ()->()) throws{
        if let user = User.current {
            let url = "\(API.addressUrl)/\(user.id)"
            let data: [String: Any] = self.toDictionary() as! [String : Any]
            Alamofire.request(url, method: .post, parameters: data).validate().responseString(completionHandler: { response in
                switch response.result {
                    case .success:
                        if let oldAddress = user.shippingAddress {
                            oldAddress.mr_deleteEntity(in: AppDelegate.getContext())
                        }
                        user.shippingAddress = self
                        try! AppDelegate.getContext().save()
                        onSuccess()
                    case .failure( _):
                        onFailure()
                }
            })
        } else {
            throw AddressErrors.noUserFound
        }
    }
}

self.toDictionary doesn't cause property mapping to be called

amgadserry commented 6 years ago
    open func toDictionary(_ conversionOptions: ConversionOptions = .DefaultSerialize) -> NSDictionary {
        let keys = Array(self.entity.attributesByName.keys)
        return self.dictionaryWithValues(forKeys: keys) as NSDictionary
    }

as I see here conversionOptions are ignored and the dictionaryWithValues used anyways @evermeer is that the intended behavior if not i could look into fixing it and make a PR

evermeer commented 6 years ago

Ah, you are right, this is something I did not implement for CoreData. It would be awesome If you could make a pull request for this.