Sunday, 8 September 2013

NSManagedObjectContext attributes of object nil

NSManagedObjectContext attributes of object nil

pI am trying to work with core data and already am able to store records
of my class Event.m. An event has a timestamp an a note./p pBut as i try
to read all entries in my table view to display them, I see that every
timestamp and note is nil (debug). Strange thing is, that my array I get
has the right size (number of entries) and also the table rows are created
(only no note si visible)./p pHere some of my code:/p precode-
(void)fetchRecords { // Define our table/entity to use NSEntityDescription
*entity = [NSEntityDescription entityForName:@Event
inManagedObjectContext:self.managedObjectContext]; // Setup the fetch
request NSFetchRequest *request = [[NSFetchRequest alloc] init]; [request
setEntity:entity]; // Define how we will sort the records NSSortDescriptor
*sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@timeStamp
ascending:NO]; NSArray *sortDescriptors = [NSArray
arrayWithObject:sortDescriptor]; [request
setSortDescriptors:sortDescriptors]; // Fetch the records and handle an
error NSError *error; NSMutableArray *mutableFetchResults =
[[self.managedObjectContext executeFetchRequest:request error:amp;error]
mutableCopy]; if (!mutableFetchResults) { NSLog(@Error fetching data.); }
// Save our fetched data to an array [self setEventArray:
mutableFetchResults]; NSLog(@Records found: %i, [eventArray count]); }
/code/pre pAnd this is where I save my object:/p precode- (void)
saveEntry: (NSString *) note { Event *event = (Event
*)[NSEntityDescription insertNewObjectForEntityForName:@Event
inManagedObjectContext:managedObjectContext]; [event setTimeStamp: [NSDate
date]]; [event setNote:@test]; NSError *error; if (![managedObjectContext
save:amp;error]) { NSLog(@Could not save entry.); } NSLog(@Entry saved.);
[eventArray insertObject:event atIndex:0]; } /code/pre pNote that there
are two different views, from which I access Core Data./p pAnd this is
where I get my MOC in this views:/p precode- (void)viewDidLoad { [super
viewDidLoad]; AppDelegate *appDelegate = (AppDelegate *)[[UIApplication
sharedApplication] delegate]; managedObjectContext =
appDelegate.managedObjectContext; } /code/pre pa
href=http://stackoverflow.com/questions/7199143/core-data-attribute-changes-to-nil-arc-relatedHere
I was reading/a, that it could come frome the MOC itself. This is how I
hold my variable in SecondViewCont/p precode#import Event.h #import
lt;CoreData/CoreData.hgt; #import AppDelegate.h @interface
SecondViewController : UITableViewController { NSManagedObjectContext
*managedObjectContext; NSMutableArray *eventArray; } @property (nonatomic,
strong) NSManagedObjectContext *managedObjectContext; @property
(nonatomic, strong) NSMutableArray *eventArray; - (void) fetchRecords;
@end /code/pre pAnd in my .m file I have:/p precode@synthesize
managedObjectContext, eventArray; /code/pre pThen I call
codefetchRecords/code./p pAny ideas why I have this problem?/p

No comments:

Post a Comment