1

G'day All

I'm working on a CoreData driven app which starts with an empty CoreData store that I populate from a StackMob application.

I have a subclass of UITableView that fetches & presents my data as I want it to but I'm a bit puzzled about when I should best go get the initial data from StackMob. When I triggered populating my CoreData store (from a small plist file & only for testing the view) in applicationDidFinishLaunching my app spent an long time displaying the default screen & I expect that will be even longer with real data fetched from the web. I'm considering changing this method on my UITableView sub-class...

- (NSFetchedResultsController *)frc
{
    if (_frc) return _frc;

    ...

    return _frc;
}

to...

- (NSFetchedResultsController *)frc
{
    if (_frc) return _frc;

    ...

    if ([[_frc fetchedObjects] count] == 0) {
        // Spawn NSOperation to get initial data from StackMob.
        // & use it to populate my CoreData store.
    }

    return _frc;
}

in which case I'd make the NSOperation a sub-class that I could re-use for subsequent data updates. I'm checking with [[_frc fetchedObjects] count] == 0 because I'm fetching all data from the entity.

Is a good approach to take? If not what would be a better approach?

I'm hoping to provode a user experience like I've seen on some apps I use where item appear on the 'home' screen as it is downloaded & added to the CoreData store.

Cheers & TIA, Pedro :)

Taylor Leese
  • 51,004
  • 28
  • 112
  • 141
Pedro
  • 878
  • 1
  • 12
  • 29
  • Are you following one of the awesome core data tutorials that walk you through an end-to-end scenario of using Xcode to define the CoreData schema (like http://cocoadevcentral.com/articles/000085.php) or have you been handed your data structure and just working on the code interface? – bmike Aug 30 '11 at 19:34
  • My code is based on Apple examples & a few online tutorials (including by Marcus Zarra) & very close to what ScanPlayGames offered below. I've edited the question to make what I'm really asking clearer. – Pedro Aug 30 '11 at 20:03

1 Answers1

1

First, create an NSPredicate to fetch your information from Core Data (assuming its an NSSet, in this case):

NSMutableSet yourMutableDataSetYouGotFromCoreData = [self.yourCoreDataObject mutableSetValueForKey:@"yourCoreDataSetData"];

NSPredicate *yourDataFilter = [NSPredicate predicateWithFormat:@"SELF IN %@",yourMutableDataSetYouGotFromCoreData];

Next, create your fetche results controller using the predicate

// Fetched results controller
NSFetchedResultsController *yourFetchedResults = [YOURCOREDATAOBJECT fetchRequestAllGroupedBy:nil 
                                                                                                withPredicate:supplyRegisterFilter];

Next, feed this information to your table

[self.yourTable updateTableData:yourFetchedResults];

Now, in your table, where you create the cell data content - use something like this to get the data out of your fetched results controller

-(void) updateTableData:(NSFetchedResultsController*)fetched
{
       self.fetchedResultsController = fetched;

       if(self.fetchedResultsController)
          [self.tableView reloadData];

 }


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.fetchedResultsController.fetchedObjects count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView 
     cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YourCustomCellType *cell = (YourCustomCellType *)    [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (!cell)
    {
        NSArray *topLevelItems = [cellLoader instantiateWithOwner:self options:nil];
        cell = [topLevelItems objectAtIndex:0];
    }

    [self configureCellData atIndexPath:indexPath];

    return cell;
}

- (void) configureCellData:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
    YourCustomCellType *customCell = (YourCustomCellType *)cell;
    id obj = [[fetchedResultsController fetchedObjects] objectAtIndex:indexPath.row];
    [customCell setCellDataFromId:obj];
}
Delete
  • 922
  • 8
  • 12
  • I'm doing all of that (except I have no predicate because I want all items in the entity I'm querying) & it works when I have data. It also works with no data & displays an empty UITableView. My app needs to download some data from StackMob the first time it runs & when to trigger that is the bit my question was about. I'll edit the question to make that clearer. – Pedro Aug 30 '11 at 19:37
  • Thanks for trying to help. I've edited the question & take it on the chin for not making it clear enough to begin with. – Pedro Aug 30 '11 at 19:57