IOS Core data fetch-request - sorting

I use a background thread to get a limited number of records sorted by date.

Everything works fine until I delete the entry in the user interface thread (tableview).

//this is done in the background thread
NSFetchRequest *frequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"MyEntity" 
                                            inManagedObjectContext:self.managedObjectContext];
[frequest setEntity:entity];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]
                                    initWithKey:@"date"     
                                    ascending:NO];

NSArray *descriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[frequest setSortDescriptors:descriptors];

[frequest setFetchOffset:fetchOffset];    
[frequest setFetchLimit:20];

[frequest setResultType:NSManagedObjectIDResultType];

 NSError *fetchError;
 NSMutableArray *mutableFetchResults = [[self.managedObjectContext executeFetchRequest:frequest 
                                                                               error:&fetchError] mutableCopy];

Background thread registered for NSManagedObjectContextDidSaveNotification and performs the following selector

//this is done in the background thread
-(void) didSavePersistenceStore:(NSNotification *)notification
{
    [self.managedObjectContext mergeChangesFromContextDidSaveNotification:notification];
}

Problem: After deleting a record, subsequent fetch results are no longer sorted by date.

What am I missing?

+3
source share
2 answers

First make sure you are not using managedObjectContext from the wrong thread. You can call executeBlock to do this in the appropriate context.

-, , , . , , .

, . .

0

Apple:

If you set the value to NSManagedObjectIDResultType, this will demote any sort orderings to "best efforts" hints if you do not include the property values in the request.

includePendingChanges NO.

0

All Articles