Skip to content

Instantly share code, notes, and snippets.

@mikekatz
Created July 3, 2012 15:53
Show Gist options
  • Save mikekatz/3040629 to your computer and use it in GitHub Desktop.
Save mikekatz/3040629 to your computer and use it in GitHub Desktop.
Creating a KCSCachedStore and using it for query
- (id) init
{
if (self = [super init]) {
self.tableValues = [NSArray array];
}
return self;
}
- (void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self refreshData];
}
- (void) refreshData {
KCSCollection* collection = [KCSCollection collectionFromString:@"<# Collection Name #>" ofClass:[<# The KCSPersistable class for the collection #> class]];
KCSCachedStore* updateStore = [KCSLinkedAppdataStore storeWithOptions:[NSDictionary dictionaryWithObjectsAndKeys:collection, KCSStoreKeyResource, [NSNumber numberWithInt:KCSCachePolicyNetworkFirst], KCSStoreKeyCachePolicy, nil]];
KCSQuery* query = [KCSQuery query];
KCSQuerySortModifier* sortByDate = [[KCSQuerySortModifier alloc] initWithField:@"<# The name of the Date field#>" inDirection:kKCSAscending];
[query addSortModifier:sortByDate]; //sort the return by the date field
[query setLimitModifer:[[KCSQueryLimitModifier alloc] initWithLimit:10]]; //just get back 10 results
[updateStore queryWithQuery:query withCompletionBlock:^(NSArray *objectsOrNil, NSError *errorOrNil) {
if (objectsOrNil) {
self.tableValues = objectsOrNil;
[self.tableView reloadData];
}
} withProgressBlock:nil];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.tableValues.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [self.tableValues objectAtIndex:indexPath.row];
return cell;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment