Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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