开发者

NSFetchedResultsController predicate is a little too real time...?

So I've got an NSFetchedResultsController that I activate on ViewDidLoad with the managedobjectcontext that has been passed on from the appdelegate on load.

I put a predicate on some field let's call it "sectionNumber" and say it needs to equal 1 in my predicate.

NSFetchResultsController works fine until I add a new object to the MOContext...

I use MyMO *newObj = [NSEntityDescription insertnewentity]...

start filling the different fields [newobj setName:@"me"]; [newobj setAge:12];

etc...

Once I put [newobj setSectionNumber:1] - it finds it at that very instant and causes the app to crash with different weird errors that all lead to EXC_BAD_ACCESS.

All of this happens on the MAIN THREAD.

Any ideas why? How could one get around that?

UPDATE: It only happens when I use my saveMOC method which is called at the end of an NSXMLParser specific thread I spawned off. The saveMOC is called on a successful parse with the [self performSelectorOnMainThread].... If i just added the extra managedobject via ViewDidLoad (just to check weather this is related somehow to to threading) the problem does NOT occur.

So it's obviously something with the new thread even tho the selector should have been run on the main thread.

UPDATE #2: This is my spawned thread for the XML Parser:

-(void)getAndParseXML { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

DLog(@"Online storage");
NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:theUrl];

XMLTranslator *translator = [[XMLTranslator alloc] init];
[parser setDelegate:translator];
if ([parser parse]) {
        //success call MOC change routine on main thread
    DLog(@"success parsing");
    [self performSelectorOnMainThread:@selector(saveMOC:) withObject:translator waitUntilDone:NO];
} else {
    DLog(@"error: %@",[parser parserError]);
}


[parser setDelegate:nil];
[parser release];
DLog(@"XML parsing completed");
[pool release];

}

Then this is my saveMOC:

-(void)saveMOC:(XMLTranslator*)translator {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
for (NSDictionary *dict in [translator retrievedData]) {

    APost *newPost = [NSEntityDescription insertNewObjectForEntityForName:@"APost"
                                                     inManagedObjectContext:managedObjectContext];

    //parse time into NSDate
    [newPost setTime:[formatter dateFromString:[dict objectForKey:@"time"]]];


    //author, category, description
    [newPost se开发者_如何学GotAuthor:[dict objectForKey:@"author"]];
    [newPost setCategory:[dict objectForKey:@"category"]];
    [newPost setContent:[dict objectForKey:@"description"]];

    //create a post id so that the validation will be alright
    [newPost setPostid:[NSNumber numberWithInteger:[[dict objectForKey:@"postid"] integerValue]]];


    [newPost setSectionDesignator:sectionDesignator];

}

This saveMoc method continues and has a [managedobjectcontext save:&error] and more... but it's not relevan to our case as my method crashes I've discovered thru commenting one line after another at the point where I set the sectionDesignator since it equals to the current predicate in my NSFetchedResultsController.


The problem is most likely in the NSFetchedResultsController delegate methods or the lack thereof.

When you add a new object to any context and then save the context, that changes the persistent store which triggers the FRC on any thread to begin an update of the tableview. All the index paths change, especially if you set a value for an attribute used as a sectionNameKeyPath. If the table ask for a cell during the update, it will cause a crash because the table can ask for a cell at a index path rendered invalid by the insertion of the new managed object.

You need to make sure you implement the FRC's delegate methods and that you send the table a beginUpdate message to freeze it while the FRC changes all its index paths.


I am sorry to admit that the problem this whole time was releasing an array that held the sort descriptors in the fetch request that was used within the FRC.

Looking at alot of examples I released that array tho unlike the examples I created my array with [NSArray arrayWithObject:.............];

So there was an overrelease each time the fetch request was accessed more than once.

  • Feel free to close this. Thank you everybody for your help. I discovered this when peter wrote to look at the whole stack and not just one frame.


I have further analyzed the problem and have realized it occurs inside the loop.

I have further understood that it only happens when I have more than one object, meaning that one FRC takes over after an object insertion into MOC and tries to come back to the for loop, it tries to access an object or a reference that's not there. I haven't found what object causes it and how to retain it properly.

Any suggestions?

Consider the following:

for (int i=0; i<2; i++) {
    NSLog(@"%i",i);
    APost *thePost = [NSEntityDescription insertNewObjectForEntityForName:@"HWBPost" inManagedObjectContext:managedObjectContext];
    [thePost setCategory:@"CAAA"];
    [thePost setContent:@"SSSSSS"];
    [thePost setSectionDesignator:sectionDesignator];       
}

If I change the for loop to i<1 meaning it only runs once, the app does NOT crash. As soon as it is more than one object insertion the app crashes.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜