Re: Big Core Data, countForFetchRequest, and GCD
Re: Big Core Data, countForFetchRequest, and GCD
- Subject: Re: Big Core Data, countForFetchRequest, and GCD
- From: Heath Borders <email@hidden>
- Date: Tue, 29 Mar 2011 22:39:24 -0500
For thread-safety's sake why don't you just create a separate
NSManagedObjectContext for your asyncTask? They are cheap.
-Heath Borders
email@hidden
Twitter: heathborders
http://heath-tech.blogspot.com
On Tue, Mar 29, 2011 at 4:38 PM, Steve Mykytyn <email@hidden> wrote:
> countForFetchRequest seems to bog down on really big collections of
> entities. For approx 170K entities it takes around 10 seconds on an iPhone
> 3GS to count them, which is a problem in a UITableView where you are trying
> to display a list of entities and associated counts.
>
> A GCD based solution, that queues up countForFetchRequest on a concurrent
> queue for each entity, and then updates the corresponding cell when
> finished, seems to work pretty well, although one or two big entity counts
> essentially bog down all the requests behind them. Counts are cached so
> that countForFetchRequest is only called once for any entity.
>
> Code below, can anyone suggest any further optimization or different
> approaches that might be more effective?
>
> (I'm aware of the danger of multi-threading core data - this particular
> managed object context never changes and nothing is happening to it on other
> threads when this read is going on)
>
> - (void) countEntitiesNamed:(NSString *)entityName
> inContext:(NSManagedObjectContext *)context forIndexPath:(NSIndexPath
> *)indexPath {
>
> NSNumber *rowCountObject = [self.rowCountMD valueForKey:entityName];
>
> if (rowCountObject) return;
>
> //
> http://www.mikeash.com/pyblog/friday-qa-2009-08-28-intro-to-grand-central-dispatch-part-i-basics-and-dispatch-queues.html
>
> dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,
> 0), ^{
>
> NSEntityDescription *ed;
> NSFetchRequest *frq;
>
> frq = [[[NSFetchRequest alloc] init] autorelease];
> ed = [NSEntityDescription entityForName:entityName
> inManagedObjectContext:context];
> [frq setEntity:ed];
> NSDate *startDate = [NSDate date];
> NSUInteger rowCount = [context countForFetchRequest:frq error:nil];
> NSTimeInterval timeInterval = -[startDate timeIntervalSinceNow];
> DLog(@"%0.2lf seconds for %d %@ ",timeInterval,rowCount,entityName);
>
> dispatch_async(dispatch_get_main_queue(), ^{
> [rowCountMD setValue:[NSNumber numberWithInteger:rowCount]
> forKey:entityName];
> UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
> cell.detailTextLabel.text = [NSString stringWithFormat:@"%@ objects",
> [NSNumberFormatter localizedStringFromNumber:[NSNumber
> numberWithInteger:rowCount]
> numberStyle:NSNumberFormatterDecimalStyle]];
>
> });
> });
>
>
> }
> _______________________________________________
>
> Cocoa-dev mailing list (email@hidden)
>
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
>
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden