Coredata on iPhone, setFetchBatchSize & setPropertiesToFetch in a single request

I am writing a coredata-based iPhone application that displays recipes. To improve performance, when I display them in a TableView, I want to enable batch processing (-setFetchBatchSize :) and select only the "name" attribute (-setPropertiesToFetch :). When I include both, this does not work and the list is empty. As soon as I comment on one of the lines noted in the code below, it works fine.

What am I missing here? Impossible to have both?

NSFetchRequest *fetchRequest = [[[NSFetchRequest alloc] init] autorelease]; [fetchRequest setEntity:[NSEntityDescription entityForName:@"Rezept" inManagedObjectContext:chk_context]]; // *snip* //BATCHING [fetchRequest setFetchBatchSize:25]; NSDictionary *entityProperties = [[NSEntityDescription entityForName:@"Rezept" inManagedObjectContext:chk_context] propertiesByName]; //PROPERTIES [fetchRequest setPropertiesToFetch:[NSArray arrayWithObject:[entityProperties objectForKey:@"name"]]]; 
+4
source share
4 answers

I am far from the main data expert, but I got this to work successfully in my situation. I think that the β€œconflict” between setFetchBatchSize and setPropertiesToFetch is more a side effect of the main data, and not an error.

In my case, I made two samples. In the first case, the result type was set to NSManagedObjectResultType, and I used setFetchBatchSize to limit the amount of data actively being written to memory. In the second sample, I fill out an array of titles based on a single attribute and set the result type in NSDictionaryResultType and fetchBatchSize to 0 (infinite).

Based on testing, this scenario works just fine. All records in the original selection (with actual managed objects) are corrupted and limited by memory using fetchBatchSize. The second sample returns a simple dictionary of names. This takes much less memory than repeating all the real managed objects to access the title attribute. It makes sense that fetchBatchSize is disabled for the second sample, since it returns a fully populated dictionary as a single result, and batch processing is not suitable.

I'm not sure that I am 100% understandable here (the basic data terminology is a bit secret ...), but the bottom line is that I think that everything works as intended.

+4
source

Looks like you found a bug in CoreData. You can verify this by enabling SQL logging β€” I assume that including both options creates invalid SQL code.

The option you want to use is "com.apple.CoreData.SQLDebug 1" - you can specify this from the command line or set the default value in your program.

-Wil

+2
source

Update: when I NSLog error after

 [fetchedResultsController performFetch:&error]; 

I get "NSUnderlyingException = Database looks corrupted (invalid primary key);". But I do not know what this means and what it should do with both methods.

0
source

Hunter you can use com.apple.CoreData.SQLDebug 1 on the phone, but not using the simulator

0
source

All Articles