When to trigger downloading StackMob initial data to a CoreData app
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 UITableVi开发者_开发问答ew 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 :)
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];
}
精彩评论