Reputation: 925
I've just been looking at Objective-c for less than a month and am definitely a newby.
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.
Herewith the code - the problem lines are:
NSManagedObjectContext *context = [Sens managedObjectContext];
Sens *newSens = [NSEntityDescription insertNewObjectForEntityForName:@"Sens" inManagedObjectContext:context];
The entire section of code is:
#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 < [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 < [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:&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);
}
Thanks - I appreciate the help!
Upvotes: 3
Views: 3427
Reputation: 90117
If you are using a Core Data template from apple the easiest way is to get it from the AppDelegate, like this:
context = [(AppDelegate_Shared *)[[UIApplication sharedApplication] delegate] managedObjectContext];
a cleaner solution would be to create a instance variable and synthesized g/setters.
You can then pass a NSManagedObjectContext
instance (most likely from the AppDelegate) to the viewController when you create it.
Upvotes: 6