ios - One-To-Many Relationships Core Data -
ios - One-To-Many Relationships Core Data -
after 3 days trying lone , after reading lot of topics, impossible configure one-to-many relationships in core data.
in app, save new user info in entity called users code :
users *datausers = [nsentitydescription insertnewobjectforentityforname:@"users" inmanagedobjectcontext:_managedobjectcontext]; [datausers setusername:usernameregistration.text.capitalizedstring]; [datausers setpassword:passwordregistration1.text]; [datausers setemailaddress:mail1.text]; [datausers setrightorleft:[nsnumber numberwithinteger:rightorlefthanded.selectedsegmentindex ]]; nserror *error = nil; if(![_managedobjectcontext save:&error]) { //handle error }
now phone call these info in different view controller perform recognition username - password , access next page.
appdelegate *appdelegate = (appdelegate*) [[uiapplication sharedapplication]delegate]; _managedobjectcontext=[appdelegate managedobjectcontext]; nsfetchrequest *request = [[nsfetchrequest alloc]init]; nsentitydescription *usersdescription= [nsentitydescription entityforname:@"users" inmanagedobjectcontext:_managedobjectcontext]; [request setentity:usersdescription]; nssortdescriptor *sortdescriptor = [[nssortdescriptor alloc]initwithkey:@"username" ascending:yes]; nsarray *sortdescriptors = [[nsarray alloc]initwithobjects:sortdescriptor, nil]; [request setsortdescriptors:sortdescriptors]; nserror *error = nil; nsmutablearray *mutablefetchresults = [[_managedobjectcontext executefetchrequest:request error:&error]mutablecopy]; if(mutablefetchresults == nil) { //handle error } [self setusersdata:mutablefetchresults]; // different steps recognition
ok works each user can log in , access next page.
now , in next page user has create new session. created new entity called sessions. each user can have several sessions each session linked 1 user.
sessions *session = [nsentitydescription insertnewobjectforentityforname:@"sessions" inmanagedobjectcontext:_managedobjectcontext]; nsnumber *howmanyswingssessionsaved = [nsnumber numberwithinteger:howmanyswingssession]; [session setname:_sessionnametextfield.text]; [session sethowmanyswings:howmanyswingssessionsaved]; [session setclub:choosenclub]; nsdate *datesaved = [nsdate date]; [session setdate:datesaved]; nserror *error = nil; if(![_managedobjectcontext save:&error]) { //handle error }
each session saved. question : how can link new session created current user connected app ? want current user , add together him new session. code save each user , each session separately.
all help appreciated :)
thanks guys !!
edit : have created relationship in xcdmodel
i want current user , add together him new session. code save each user , each session separately.
you haven't shown model, looks need add together relationships entities. session
entity needs to-one relationship user
, , inverse relationship should to-many relationship user
session
. 1 time have that, select entities in model , utilize editor->create nsmanagedobject subclass...` command generate subclasses each entity. if @ resulting header files, you'll find accessors can utilize create relationships between objects. may not need utilize them -- example, like:
sessions *session = [nsentitydescription insertnewobjectforentityforname:@"sessions" inmanagedobjectcontext:_managedobjectcontext]; session.user = thecurrentuser;
ios core-data entity-relationship
Comments
Post a Comment