objective c - Update CoreData entity obj -
i have complex coredata entity: my_entity
i receive object of type my_entity webservice.
in cases, need edit local coredata obj (my_entity) received obj.
so:
i have obj_1 in coredata
i receive obj_2 webservice.
i need update obj_1 obj_2. have set field or can assign obj_1 objectid obj_2 , save context (same context)?
since 2 separate instances, need move want o2 o1. can use routine such move attribute attribute assuming both objects of same entity class:
// use entity description entity attributes , use keys value // scan attributes nsdictionary *attributes = [[sourceentity entity] attributesbyname]; (nsstring *attribute in attributes) { id value = [sourceentity objectforkey:attribute]; if (value == nil) { continue; } nsattributetype attributetype = [[attributes objectforkey:attribute] attributetype]; switch (attributetype) { case nsstringattributetype: // value = [value stringvalue]; break; case nsinteger16attributetype: case nsinteger32attributetype: case nsinteger64attributetype: case nsbooleanattributetype: value = [nsnumber numberwithinteger:[value integervalue]]; break; case nsfloatattributetype: case nsdecimalattributetype: value = [nsnumber numberwithdouble:[value doublevalue]]; break; case nsdateattributetype: if (dateformatter != nil) value = [dateformatter stringfromdate:value]; break; default: value = @""; break; } [targetentity setvalue:value forkey:attribute]; }
note, example , need cleaned , have error handling added if intend use. also, if getting o2 via webservice json or xml, can use push json payload targetentity. assumes payload attrs align entity attrs. in case replace sourceentity json payload using block or equivalent example:
nsarray *seeddata = [nsjsonserialization jsonobjectwithdata:[nsdata datawithcontentsoffile:datapath] options:kniloptions error:&err]; [seeddata enumerateobjectsusingblock:^(id obj, nsuinteger idx, bool *stop) { ... id value = [obj objectforkey:attribute]; ... }
Comments
Post a Comment