ios - Core Data to-one relationship returns NULL -
the batting.team relationship doesn't save , returns null sometimes
about 100 'team' nsmanagedobjects saved , can log attributes expected. 15000 'batting' nsmanagedobjects saved , attributes log correctly except relationship.
i want batting.team relationship point team object has same team.teamid value batting.teamid.
when setting batting relationship, create predicate search specific team.teamid, fetch request , supposedly set team object equal batting.team relationship. relationship attribute gets set times doesn't , can't figure out
- (bool)application:(uiapplication *)application didfinishlaunchingwithoptions:(nsdictionary *)launchoptions { // override point customization after application launch. nserror *error; nsstring* datapath_teams = [[nsbundle mainbundle] pathforresource:@"teams" oftype:@"json"]; nsarray* teams = [nsjsonserialization jsonobjectwithdata:[nsdata datawithcontentsoffile:datapath_teams] options:kniloptions error:&error]; nsstring* datapath_abc = [[nsbundle mainbundle] pathforresource:@"abc" oftype:@"json"]; nsarray* batting_abc = [nsjsonserialization jsonobjectwithdata:[nsdata datawithcontentsoffile:datapath_abc] options:kniloptions error:&error]; //insert team objects nsfetchrequest *fetchrequestteams = [[nsfetchrequest alloc] init]; nsentitydescription *entityteams = [nsentitydescription entityforname:@"team" inmanagedobjectcontext:self.managedobjectcontext]; [fetchrequestteams setentity:entityteams]; (id t in teams) { team *team = [nsentitydescription insertnewobjectforentityforname:@"team" inmanagedobjectcontext:self.managedobjectcontext]; team.name = [t objectforkey:@"name"]; team.minyearid = [t objectforkey:@"minyearid"]; team.maxyearid = [t objectforkey:@"maxyearid"]; team.teamid = [t objectforkey:@"teamid"]; if (![self.managedobjectcontext save:&error]) { nslog(@"whoops, couldn't save: %@", [error localizeddescription]); } } nsarray *fetchedobjectsteams = [self.managedobjectcontext executefetchrequest:fetchrequestteams error:&error]; nslog(@"number of records in teams.json - %d", (int)[fetchedobjectsteams count]); //insert batting objects nsfetchrequest *fetchrequestbatting = [[nsfetchrequest alloc] init]; nsentitydescription *entitybatting = [nsentitydescription entityforname:@"batting" inmanagedobjectcontext:self.managedobjectcontext]; [fetchrequestbatting setentity:entitybatting]; (id b in batting_abc) { batting *batting = [nsentitydescription insertnewobjectforentityforname:@"batting" inmanagedobjectcontext:self.managedobjectcontext]; batting.playerid = [b objectforkey:@"playerid"]; batting.h = [b objectforkey:@"h"]; batting.ab = [b objectforkey:@"ab"]; batting.hr = [b objectforkey:@"hr"]; batting.rbi = [b objectforkey:@"rbi"]; batting.sb = [b objectforkey:@"sb"]; batting.r = [b objectforkey:@"r"]; batting.bb = [b objectforkey:@"bb"]; batting.so = [b objectforkey:@"so"]; batting.yearid = [b objectforkey:@"yearid"]; batting.teamid = [b objectforkey:@"teamid"]; nspredicate *teamidpredicate = [nspredicate predicatewithformat:@"teamid == %@", [b objectforkey:@"teamid"]]; [fetchrequestteams setpredicate:teamidpredicate]; nsarray *fetchedspecificteam = [self.managedobjectcontext executefetchrequest:fetchrequestteams error:&error]; batting.team = [fetchedspecificteam firstobject]; if (![self.managedobjectcontext save:&error]) { nslog(@"whoops, couldn't save: %@", [error localizeddescription]); } } fetchedobjectsbatting = [self.managedobjectcontext executefetchrequest:fetchrequestbatting error:&error]; (batting *b in fetchedobjectsbatting) { nslog(@"playerid: %@", b.playerid); nslog(@"yearid: %@", b.yearid); nslog(@"teamid: %@", b.teamid); nslog(@"team: %@\n\n", b.team.name); } return yes; }
from screenshot of model, configured each team
can have 1 batting
. each time set batting.team
given team
, coredata remove link team
current batting object (thereby setting batting
object's team
nil).
you should define team
entity's batting
relationship "to-many" (each team
can have many battings
) using data model inspector on right hand side when in model editor:
Comments
Post a Comment