1
votes

I have a Core Data financial app that needs to accumulate sales quantities contained in the Trans Entity for each product and then update the totals into the appropriate attribute of the Product Entity.

I am able to achieve this by nesting a for (transArray) inside and tableView (product). However I need to sort and format the tableView based on the results first.

General Question: Can fetched results be used without tableViews?

- (void)calculateAmounts {
     NSIndexPath *indexPath=0;

    for (Product *product in self.fetchedResultsController.fetchedObjects){      // All product records
        selectedProduct = [self.fetchedResultsController objectAtIndexPath:indexPath];

        // >>>>>NSLog shows correct number of object, however selectedProduct @ Index Path Are NULL

        for (id product1 in transProductArray)  {              // An array of all of the trans for product

            if ((NSNull *)product1 == [NSNull null]) {

            }
            else if ([product1 isEqualToString:selectedProduct]) {
                float qty = [@"1" floatValue];
                NSNumber *numQty=[NSNumber numberWithFloat:qty];   // Update quantity sold in product by 1
                NSNumber *quantity = [NSNumber numberWithFloat:([selectedProduct.quantitySold floatValue] + [numQty floatValue])];
                selectedProduct.quantitySold = quantity;
                [self.product.managedObjectContext save:nil];
            } 
        }    // Next Trans
    }        // Next Product
}
3

3 Answers

1
votes

This is a good question. Not (clearly) knowing the implementation, I will bet it is possible to use fetched results outside a tableview. However, the Overview of NSFetchedResultsController documentation has this as the first line:

You use a fetched results controller to efficiently manage the results returned from a Core Data fetch request to provide data for a UITableView object.

While table views can be used in several ways, fetched results controllers are primarily intended to assist you with a master list view.

This infers that the two objects are linked for efficiency and ease of use. Plus, there are monitors under-the-hood to watch for changes, etc.

What you may be looking for is a 'plain-old' NSFetchRequest. A tutorial.

1
votes

Hi I am not to sure whether you still require an answer to this however I had the same issue myself. I overcame as described in my own question on the subject.

Display multiple core data entities objects in 1 Non-Table View Controller

I inserted a small tableview into my VC and set it to alpha and used the - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath to enable displaying details in labels.

This was the only way I could overcome it. Hope this helps.

0
votes

Thanks for the answer. Like a lot of things the answer was too easy! It's about the fetch, not the tableview. Just do a fetch into an array, then loop though the array to accumulate your values: Also useful for finding a specific object or output to a CSV.

NSError *error;
NSNumber *total=0;

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
 NSEntityDescription *entity = [NSEntityDescription
 entityForName:@"TransDetail" inManagedObjectContext:managedObjectContext];
 [fetchRequest setEntity:entity];

 NSArray *fetchedObjects = [managedObjectContext executeFetchRequest:fetchRequest error:&error];
 for (TransDetail *trans in fetchedObjects) {

       total = total + trans.amount;
  }