Question

I have an entity called "Records", and it has an attribute named "amount" of NSDecimalNumber class.

And of course "Record" has other attributes such as "name","date", and so on.

Now I need to fetch only the amount attribute of all "Records" to sum them up,

to get better performance I just need the value of "amount", and I don't care the name, or date.

So how should I do?

Here's my code but I guess they are not professional enough.

NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"TransferRecord"];
request.includesSubentities = NO;
[request setPropertiesToFetch:[NSArray arrayWithObject:@"amount"]];
//    [request setReturnsObjectsAsFaults:NO];  // I don't know whether I shoud use this
[request setResultType:NSDictionaryResultType];
NSError *error = nil;
NSArray *temp = [self.fetchedResultsController.managedObjectContext executeFetchRequest:request error:&error];
if (temp) {
//        NSDecimalNumber *allTrans = [NSDecimalNumber zero];
//        for (NSDecimalNumber *one in [temp valueForKey:<#(NSString *)#>)
    NSLog(@"%@",[temp description]);

And I'm not clear about what the "faults" means.

Was it helpful?

Solution

Eno,

How do you know that the standard fetch is not fast enough for your needs? Have you identified the fetch, using Instruments, as the problem? (I ask because the standard way to make CD apps run faster, particularly on iOS, is to fetch more data by using simpler predicates. You then refine the query using set operations on the items in RAM.)

If you think about it, you'll see why. Rows are stored in contiguous bytes on disk. The rows can frequently be found in the same disk block. Hence, a single fetch can potentially get many rows. The disk fetch is the slow part of any database query. On iOS, the flash is quite slow. It isn't designed for high performance scatter-gather database operations. IOW, an iOS device is not and SSD for its own OS. BTW, a fetch on flash will bring upwards of 128k-256k bytes into the buffers. Hence, getting more rows is easy and comparatively fast.

Your code above is basically correct.

You need to go read the documentation about Core Data faults. It is a fundamental concept in the system. Apple's documentation is quite clear on the nature of a fault. Stack Overflow is the wrong place to ask for basic information covered well in any system's documentation.

Andrew

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top