开发者

Core Data object gets invalidated

开发者 https://www.devze.com 2023-01-30 11:22 出处:网络
I have a problem with some Core Data objects that are somehow invalidated. The managed object context is in the app delegate and I use it in a view table to fetch \'notes\' objects from a database and

I have a problem with some Core Data objects that are somehow invalidated. The managed object context is in the app delegate and I use it in a view table to fetch 'notes' objects from a database and display them. I build an array for the sections (today, yesterday, etc.) and for each section an array with the notes in the section like this:

// in the .h file
NSMutableArray* data; // An array containing an array of thoughts for each section.
@property (nonatomic, retain, readonly) NSManagedObjectContext* objectContext;

// in the .m file, when loading the view
ThoughtsAppDelegate* appDelegate = (ThoughtsAppDelegate*)[[UIApplication sharedApplication] delegate];
objectContext = appDelegate.managedObjectContext;

NSEntityDescription* descriptor = [[NSEntityDescription entityForName:@"Note"                       
                                   inManagedObjectContext:objectContext] autorelease];
NSFetchRequest* request = [[NSFetchRequest alloc] init];
[request setEntity:descriptor];
NSError* error;
NSArray* notes = [objectContext executeFetchRequest:request error:&error];
// example for one section
data = [[NSMutableArray alloc] init];
NSMutableArray* ccurrentSection = [[NSMutableArray alloc] init];
[data addObject:currentSection];
for(Note* t in notes) 
    [currentSection addObject:t];

When the view loads the first 5 notes are displayed (the rest don't fit in the view) and all is OK. But when I scroll down to view the next notes I get an

NSObjectInaccessibleException The NSManagedObject with ID... has been invalidated.

This happens for all objects in the array.

How is this possible? I checked and don't reset/release the context开发者_Go百科. Or is it bad to store a Core Data object and refer to it later?

Edit: this seems to happen also if I don't scroll and want to display details about a note when it's selected. Seems that as soon the first notes are displayed they're invalidated.


It would appear to be something with the way you manage the notes objects, but the code that is doing this is not in your example. The notes array is an autorelease array so unless you are retaining it somewhere it may be releasing before your load the next section from it.

0

精彩评论

暂无评论...
验证码 换一张
取 消