objective c - CoreData for Adding under an Object in Relationship -


this easier iphone devs familiar coredata not , know how access data within crucial deadline.

this core data once set nsmutablearray , pulled nslog

questions list km: (     "<question: 0x8589630> (entity: question; id: 0x8588820 <x-coredata://fdc76465-4884-473a-b46a-3451bcb74f5c/question/p1> ; data: {\n    orderid = 0;\n    project = \"0x818e300 <x-coredata://fdc76465-4884-473a-b46a-3451bcb74f5c/project/p1>\";\n    question = \"what ...? \";\n    strategy = \"<relationship fault: 0x8264930 'strategy'>\";\n})",     "<question: 0x85898d0> (entity: question; id: 0x8588830 <x-coredata://fdc76465-4884-473a-b46a-3451bcb74f5c/question/p2> ; data: {\n    orderid = 1;\n    project = \"0x818e300 <x-coredata://fdc76465-4884-473a-b46a-3451bcb74f5c/project/p1>\";\n    question = \"how ...? \";\n    strategy = \"<relationship fault: 0x826fa60 'strategy'>\";\n})",     "<question: 0x8589930> (entity: question; id: 0x8588840 <x-coredata://fdc76465-4884-473a-b46a-3451bcb74f5c/question/p3> ; data: {\n    orderid = 2;\n    project = \"0x818e300 <x-coredata://fdc76465-4884-473a-b46a-3451bcb74f5c/project/p1>\";\n    question = \"where ...? \";\n    strategy = \"<relationship fault: 0x826c430 'strategy'>\";\n})" ) 

i able pull array below:

// fetch , sort questions     nsfetchrequest *request = [[nsfetchrequest alloc] init];     nsentitydescription *entity = [nsentitydescription entityforname:@"question"  inmanagedobjectcontext:self.managedobjectcontext];     [request setentity:entity];      nssortdescriptor *sortdescriptor = [[nssortdescriptor alloc] initwithkey:@"orderid" ascending:yes];     nsarray *sortdescriptors = [[nsarray alloc] initwithobjects:sortdescriptor, nil];     [request setsortdescriptors:sortdescriptors];      nserror *error = nil;     nsmutablearray *mutablefetchresults = [[self.managedobjectcontext executefetchrequest:request error:&error] mutablecopy];     if (mutablefetchresults == nil) {         // handle error.         nslog(@"no results in questions array.");     }      [self setquestionslist:mutablefetchresults]; 

here question.h or question definitions:

#import <foundation/foundation.h> #import <coredata/coredata.h>  @class project, strategy;  @interface question : nsmanagedobject  @property (nonatomic, retain) nsnumber * orderid; @property (nonatomic, retain) nsstring * question; @property (nonatomic, retain) project *project; @property (nonatomic, retain) nsset *strategy; @end  @interface question (coredatageneratedaccessors)  - (void)addstrategyobject:(strategy *)value; - (void)removestrategyobject:(strategy *)value; - (void)addstrategy:(nsset *)values; - (void)removestrategy:(nsset *)values;  @end 

here strategy.h definitions:

#import <foundation/foundation.h> #import <coredata/coredata.h>  @class question;  @interface strategy : nsmanagedobject  @property (nonatomic, retain) nsstring * strategy; @property (nonatomic, retain) question *question;  @end 

you can see in nslog of data there 3 questions

  1. question = \"what ...?
  2. question = \"how ...?
  3. question = \"where ...?

it shows there strategy section in relationship:

strategy = \"<relationship fault: 0x826c430 'strategy'>\ 

what need able access strategy , add single object of type nsstring.

once assigned need pull strategies nsmutablearray pushing uitableview.

i know lot take in apple guru sounds cake , ice cream.

if more information needed answer, please let me know in comments....a great stackoverflow member once told me

"sometimes hardest part finding right way ask question."

any appreciated!

i not sure if understand question correctly, accessing strategies question done via accessor methods. (note strategy to-many relationship, better name strategies, , use here)

question *thequestion = ... // nsset *strategiesasset = thequestion.strategies; // set of related strategy objects // or: nsarray *strategiesasarray = [thequestion.strategies allobjects]; // array of related strategy objects. 

to add strategy, have create object:

strategy *newstrategy = [nsentitydescription insertnewobjectforentityforname:@"strategy" inmanagedobjectcontext:context]; 

and set relationship. can either set

newstrategy.question = thequestion; 

or alternatively,

[question addstrategyobject:newstrategy]; 

due nature of inverse relationships, these 2 operations equivalent, 1 implies other.


Comments