ios - Populating One to Many Relationship JSON to Core Data -


having problems populating 1 many core data model through json.

background:

two entities:      workouttype     attributes:         -workouttype     relationship: workouts     destination: workoutset     inverse: workouttype      workoutset     attributes:         -workoutname     relationship: workouttype     destination: workouttype     inverse: workouts 

below json:

[   { "workouttype": "strength",            "workouts":             [   { "workoutname": "strength 1"},                 { "workoutname": "strength 2"},                 { "workoutname": "strength 3"},                 { "workoutname": "strength 4"}                 ],   },   { "workouttype": "core",            "workouts":             [   { "workoutname": "core 1"},                 { "workoutname": "core 2"},                 { "workoutname": "core 3"},                 { "workoutname": "core 4"}                 ],  } ] 

the json parses correctly, problem each workouttype (core , strength) there should 4 workouts (strength 1, strength 2,....) ..(core 1, core 2,....).

what achieve after viewing sqlite database created strength has 3 workouts , core has 5 workouts. each time compile code numbers change.

the code using below:

    nsstring* thirddatapath = [[nsbundle mainbundle] pathforresource:@"workouts" oftype:@"json"];     nsarray* workouts = [nsjsonserialization jsonobjectwithdata:[nsdata datawithcontentsoffile:thirddatapath]                                                        options:kniloptions                                                          error:&err];     nslog(@"imported workouts: %@", workouts);      [workouts enumerateobjectsusingblock:^(id obj, nsuinteger idx, bool *stop) {          workouttype *workouttype = [nsentitydescription                           insertnewobjectforentityforname:@"workouttype"                           inmanagedobjectcontext:context];         workouttype.workouttype = [obj objectforkey:@"workouttype"];            nsarray *workoutsarray = [obj objectforkey:@"workouts"];         [workoutsarray enumerateobjectsusingblock:^(id workoutsobj, nsuinteger workoutsidx, bool *workoutsstop) {               workoutset *workoutset = [nsentitydescription                                       insertnewobjectforentityforname:@"workoutset"                                       inmanagedobjectcontext:context];               //not sure if line below correct, not sure if should setting relationship here workoutset.workouts = ....              workoutset.workouttype = workouttype;             workoutset.workoutname = [workoutsobj objectforkey:@"workoutname"];         }];          nserror *error;         if (![context save:&error]) {             nslog(@"whoops, couldn't save: %@", [error localizeddescription]);         }      }]; 

be easy please newbie. within code have added comment think may making mistake.

will grateful help. thank you.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -