FRZDatabaseViewMapper
是一个用于管理多个 YapDatabaseViewMappings 的类。
特性
- 处理所有动画更新用户界面的样板代码
- 对更新的视图类型不可知 - 可以直接与
UITableView
和UICollectionView
一起工作,但也可管理任何遵循FRZDatabaseViewMappable
的视图。 - 支持在同一个视图中使用多个视图映射,这意味着您可以在多个表格视图部分中显示相同的数据库对象
- 支持在不同视图映射之间动态切换(例如在激活搜索时)
- 处理
UICollectionView
中的边缘情况/难以找到的Bug
在 Forza Football 中需要支持在单个 UICollectionView
的多个部分中显示相同的比赛时,FRZDatabaseViewMapper
应运而生。我们很快意识到,由于它处理了更新 UI 的样板代码,所以它在需要使用 YapDatabase 视图的每个地方都很有用。现在它已在我们的一些应用程序中使用,并使创建这些视图变得容易得多!
使用示例
@interface MyTableViewController : UITableViewController
@property (nonatomic, strong) FRZDatabaseViewMapper *viewMapper;
@end
@implementation MyTableviewcontroller
- (void)viewDidLoad {
[super viewDidLoad];
self.viewMapper = [[FRZDatabaseViewMapper alloc] initWithDatabase:database];
// Since self.view is a UITableView, viewMapper can update it automatically. It does support any object that conforms to FRZDatabaseViewMappable.
self.viewMapper.view = self.view;
// Now, initialize your view mappings normally. If needed, create multiple view mappings and they will be shown in the table view in the same order as in viewMapper.activeViewMappings!
YapDatabaseViewMappings *mappings = [YapDatabaseViewMappings mappingsWithGroups:@[@"group_1", @"group2"] view:"my_database_view"];
self.viewMapper.activeViewMappings = @[mappings];
// All done! self.view will now be automatically updated when the underlying my_database_view changes.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [self.viewMapper numberOfSections];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.viewMapper numberOfItemsInSection:section];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
MyObject *object = [self.viewMapper objectAtIndexPath:indexPath];
// Dequeue/configure cell normally
}
@end
如果您自己的应用程序管理自己的长期 UI 连接(如 此处 所述),则可以这样让视图映射使用该连接而不是管理自己的连接
// Must be a long-lived read transaction
YapDatabaseConnection *connection = ...;
// The NSNotification that is posted when connection is updated. The posted notification must have connection set as "object", and contain the changes generated by [connection beginLongLivedReadTransaction] in userInfo.
NSNotificationName updateNotificationName = @"notification";
self.viewMapper = [[FRZDatabaseViewMapper alloc] initWithConnection:connection updateNotificationName:updateNotificationName];