WilddogObjects是一个回调,用几个类配合wilddog实现一个同步的NSDictionary;
// 在viewDidLoad中
//初始化 WDGApp
WDGOptions *option = [[WDGOptions alloc] initWithSyncURL:@"https://<YOUR-FIREBASE-APPID>wilddogio.com"];
[WDGApp configureWithOptions:option];
//获取一个指向根节点的 WDGSyncReference 实例
WDGSyncReference *rootRef = [[WDGSync sync] reference];
WDGSyncReference *refStaff = [rootRef child:@"stuff"];
NSMutableDictionary * dictionary = [NSMutableDictionary dictionary];
WilddogCollection * collection = [[WilddogCollection alloc] initWithNode:refStaff dictionary:dictionary type:[User class]];
[collection didAddChild:^(User * user) {
// created remotely or locally, it is called here
NSLog(@"New User %@", user);
[self.tableView reloadData];
}];
User * me = [User new];
me.name = @"me";
[collection addObject:me];
###我们怎么去运行一个tableView呢
就是使用字典的所有数据去加载它。 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [self.dictionary.allValues count]; }
告诉你什么类型的数据在集合中传递,另外,你能够使用'factory'初始化方法去创建WilddogCollection
self.collection = [[WilddogCollection alloc] initWithNode:wilddog dictionary:dictionary factory:^(NSDictionary* value) {
return [User new];
}];
在你的对象中,wilddog内部会调用setValuesForKeysWithDictionary
去设置值
你的类应该实现 Objectable
协议,以便WilddogCollection
把它转化为字典
-(NSDictionary*)toObject {
return [self dictionaryWithValuesForKeys:@[@"name"]];
}
###另外,移除和更新所有处理
当他们改变时使用didAddChild
, didRemoveChild
和 didUpdateChild
去修改
self.connection = [[WilddogConnection alloc] initWithWilddogName:@"mywilddogname" onConnect:^{
[self connectToLobby];
} onDisconnect:^{
[self showDiconnectedScreen];
}];
## 支持
如果在使用过程中有任何问题,请提 issue ,我会在 Github 上给予帮助。
We would like to thank the following projects for helping us achieve our goals:
Open Source:
- ios-firebase-objects FirebaseObjects is a repo with a few classes to make common firebase tasks easier