objective c - Can't Access Entity In Core Data From Another View Controller -


i novice objective-c programmer , core data user. first time using core data. trying put entities in core data database , retrieve , edit them in view controller.
here code in viewcontroller.m:

- (void)viewdidload {     [super viewdidload];     // additional setup after loading view, typically nib.     nsfilemanager *filemanager = [nsfilemanager defaultmanager];     nsurl *documentsdirectory = [[filemanager urlsfordirectory:nsdocumentdirectory indomains:nsuserdomainmask] firstobject];      nsstring *documentname = @"mydocument";     nsurl *url = [documentsdirectory urlbyappendingpathcomponent:documentname];     uimanageddocument *document = [[uimanageddocument alloc]initwithfileurl:url];     bool fileexists = [[nsfilemanager defaultmanager] fileexistsatpath:[url path] ];     if(fileexists)     {         [document openwithcompletionhandler:^(bool success){             if(!success) nslog(@"error (this isn't printed)");         }];     }     else     {         [document savetourl:url forsaveoperation:uidocumentsaveforcreating completionhandler:^(bool success){             if(!success) nslog(@"error (this isn't printed)");         }];          nsmanagedobjectcontext *context = document.managedobjectcontext;          questionresponse *question1 = [nsentitydescription insertnewobjectforentityforname: @"questionresponse" inmanagedobjectcontext: context];         questionresponse *question2 = [nsentitydescription insertnewobjectforentityforname: @"questionresponse" inmanagedobjectcontext: context];         questionresponse *question3 = [nsentitydescription insertnewobjectforentityforname: @"questionresponse" inmanagedobjectcontext: context];         questionresponse *question4 = [nsentitydescription insertnewobjectforentityforname: @"questionresponse" inmanagedobjectcontext: context];          question1.question = @"what favorite food?";         question2.question = @"who favorite fictional character?";         question3.question = @"name loves you:";         question4.question = @"what favorite place (real or fictional)?";          question1.order = [nsnumber numberwithint:1];         question2.order = [nsnumber numberwithint:2];         question3.order = [nsnumber numberwithint:3];         question4.order = [nsnumber numberwithint:4];         question1.group = [nsnumber numberwithint:1];         question2.group = [nsnumber numberwithint:1];         question3.group = [nsnumber numberwithint:2];         question3.group = [nsnumber numberwithint:2];         question1.display = [nsnumber numberwithbool:no];         question2.display = [nsnumber numberwithbool:no];         question3.display = [nsnumber numberwithbool:no];         question4.display = [nsnumber numberwithbool:no];          nsfetchrequest* request2 = [nsfetchrequest fetchrequestwithentityname:@ "questionresponse"];         nserror* error2 = nil;         nsarray* results = [context executefetchrequest:request2 error:&error2];         if (!results || !results.count){             nslog(@"no elements (this isn't printed)");         }         if(results.count == 4)         {             nslog(@"we have 4 entities (this printed)");         }             } } 

in answerviewcontroller.m code:

 - (ibaction)answerquestion1:(id)sender {         //file creation begins here (same in view controller)     nsfilemanager *filemanager = [nsfilemanager defaultmanager];     nsurl *documentsdirectory = [[filemanager urlsfordirectory:nsdocumentdirectory indomains:nsuserdomainmask] firstobject];     nsstring *documentname = @"mydocument";         nsurl *url = [documentsdirectory urlbyappendingpathcomponent:documentname];     uimanageddocument *document = [[uimanageddocument alloc]initwithfileurl:url];     bool fileexists = [[nsfilemanager defaultmanager] fileexistsatpath:[url path] ];     if(fileexists)     {         [document openwithcompletionhandler:^(bool success){             if(!success) nslog(@"error");         }];         nslog(@"file exists (this printed)");     }     else     {         [document savetourl:url forsaveoperation:uidocumentsaveforcreating completionhandler:^(bool success){             if(!success)nslog(@"there has been problem (this isn't printed)");         }];     }     nsmanagedobjectcontext *context = document.managedobjectcontext;     //file creation ends here      nserror *error = nil;      nsfetchrequest* request2 = [nsfetchrequest fetchrequestwithentityname:@ "questionresponse"];     nserror* error2 = nil;     nsarray* results = [context executefetchrequest:request2 error:&error2];     if (!results || !results.count){         nslog(@"no entities (this printed");     }     else     {         nslog(@"some entities (this isn't printed)");     } } 

in viewcontroller.m core data has 4 elements when try access them in answereviewcontroller.m there nothing in core data. why happening , how can access elements answereviewcontroller.m?

in order use coredata successfully, have first understand flow of it. in essence, coredata can broken down 2 main parts:

  1. the database (so .sqlite file itself)
  2. the persistent store coordinator (henceforth known psc)

what have done far in viewcontroller.m have told compiler insert 4 objects managedobjectcontext. gets done on psc level. psc acts scratchpad of database changes. until tell save it, live in psc , not in .sqlite file. need call:

[context save:&error2] 

this tell compiler push current status of psc database persists.

i hope helps you.


Comments

Popular posts from this blog

cakephp - simple blog with croogo -

How to group boxplot outliers in gnuplot -

bash - Performing variable substitution in a string -