Skip to content

Instantly share code, notes, and snippets.

@sebastienwindal
Last active August 29, 2015 14:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sebastienwindal/c84a2c25c081c023ba03 to your computer and use it in GitHub Desktop.
Save sebastienwindal/c84a2c25c081c023ba03 to your computer and use it in GitHub Desktop.
YapDatabase
YapDatabaseViewGroupingWithObjectBlock groupingBlock = ^NSString *(NSString *collection, NSString *key, id object) {
if (![object isKindOfClass:[MealLogRemote class]]) {
return nil;
}
return "all"; // we only need one section/group in this case
};
YapDatabaseViewSortingWithObjectBlock sortingBlock = ^NSComparisonResult (NSString *group, NSString *collection1, NSString *key1, MealLogRemote *obj1,
NSString *collection2, NSString *key2, MealLogRemote *obj2){
// we don't need sorting but this using a sorting for a view is mandatory, lets sort it by date
return [obj1.eatenOn compare:obj2.eatenOn];
};
YapDatabaseViewGrouping *grouping = [YapDatabaseViewGrouping withObjectBlock:groupingBlock];
YapDatabaseViewSorting *sorting = [YapDatabaseViewSorting withObjectBlock:sortingBlock];
// finally create a view of all meal logs so we can create a subset of it
YapDatabaseView *v = [[YapDatabaseView alloc] initWithGrouping:grouping sorting:sorting];
[[Database yap] registerExtension:v withName:@"all_meal_logs"];
YapDatabaseViewFiltering *filtering = [YapDatabaseViewFiltering withObjectBlock:^BOOL(NSString *group, NSString *collection, NSString *key, MealLogRemote *object) {
return [object.isPlaceholderMealLog boolValue] == FALSE && [obj.eatenOn compare:twoDaysAgoMidnigth] < 0;
}];
YapDatabaseFilteredView *filterView = [[YapDatabaseFilteredView alloc] initWithParentViewName:@"all_meal_logs"
filtering:filtering];
[[Database yap] registerExtension:nonplaceholderView withName:@"old_placeholder_meal_logs"];
[[[Database yap] newConnection] readWriteWithBlock:^(YapDatabaseReadWriteTransaction * transaction) {
[[transaction extension:@"old_placeholder_meal_logs"] removeAllObjectsInCollection:[MealLogRemote remoteModelName]];
}];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment