title: Core-data managedObjectContext - where does it come from?
body: <p>I've just been looking at Objective-c for less than a month and am definitely a newby.  </p>
  
  <p>I am trying to write some data that I have retrieved from a web service.  This data is called "Sens", and I have created the Core-data structures and NSManagedObject class - "Sens.h".  I have been trying several different ways of creating the Sens Object, but am stuck with determining the context (NSManagedObjectContext).   I read that it should come from the object (which is the code below) - not working.  I also tried self.managedObjectContext - also didn't work.  </p>
  
  <p>Herewith the code - the problem lines are:</p>
  
  <pre><code>NSManagedObjectContext *context = [Sens managedObjectContext];
  Sens *newSens = [NSEntityDescription insertNewObjectForEntityForName:@"Sens" inManagedObjectContext:context];
  </code></pre>
  
  <p>The entire section of code is:</p>
  
  <pre><code>#import "RootViewController.h"
  #import "XMLElement.h"
  #import "Sens.h"
  
  
  @implementation RootViewController
  
  @synthesize xmlDocument;
  
  
  -(void) xmlDocumentDelegateParsingFinished:(XMLDocument *)paramSender {
  NSLog(@"Finished downloading and parsing the remote XML");
  // loop through the parsed items - sens
  if ([paramSender.rootElement.name isEqualToString:@"ArrayOfSensItem"]) {
      for (int i = 0; i &lt; [paramSender.rootElement.children count]; i++) {
          XMLElement *element = [[XMLElement alloc] init];
          element = [paramSender.rootElement.children objectAtIndex:i];
  
          // we would like to create a new sens object that must be written away
          NSManagedObjectContext *context = [Sens managedObjectContext];
          Sens *newSens = [NSEntityDescription insertNewObjectForEntityForName:@"Sens" inManagedObjectContext:context];
  
          for (int j = 0; j &lt; [element.children count]; j++) {
              // another element - this is the actually interesting stuff
              XMLElement *childElement = [[XMLElement alloc]init];
              childElement = [element.children objectAtIndex:j];
  
              if (newSens != nil) {
                  // body
                  if ([childElement.name isEqualToString:@"body"])
                      newSens.body = childElement.text;
  
                  // company code list
                  newSens.company = @"";
  
                  // headline
                  newSens.heading = childElement.text;
  
                  // newsid
                  newSens.sensID = (NSNumber *)[childElement.text intValue];
  
                  // sens date
                  NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
                  [dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
                  newSens.sensDate = [dateFormat dateFromString:childElement.text];
  
                  // display
                  NSLog(@"Name: %@",childElement.name);
              }
  
              [childElement release];
          }
  
          // save the unsaved changes into the context
          NSError *savingError = nil;
          if ([context save:&amp;savingError] == YES){
              NSLog(@"Successfully saved the SENS item: %@",newSens.sensID);
          } else {
              NSLog(@"Failed to create a new SENS record.");
          }
  
          [newSens release];
  
  
  
          // release the element
          [element release];
  
      }
  }
  NSLog(@"Root element: %@",paramSender.rootElement.name);
  }
  </code></pre>
  
  <p>Thanks - I appreciate the help!</p>