我试图在两个类之间的NSDictionary
中获取值。
我在Database.m
(NSObject类)中使用此方法
+(void)fetchAllUserWithReference:(NSDictionary *)dictionary {
FIRDatabaseReference *userReference = [[[FIRDatabase database] reference] child:RTDUSER];
[userReference observeEventType:FIRDataEventTypeChildAdded withBlock:^(FIRDataSnapshot * _Nonnull snapshot) {
__block NSDictionary *dict = dictionary;
dict = snapshot.value;
} withCancelBlock:nil];
}
我想在Dashboard.m
(UIView Controller)类中获取NSDictionary数据,就像这样..
-(void)retrieveUsers {
NSDictionary *dict = [[NSDictionary alloc] init];
[Database fetchAllUserWithReference:dict];
NSLog(@"DICT %@",dict);
}
为什么我的NSDictionary
类Dashboard.m
总是为空?我哪里错了?
您可以使用完成处理程序在执行块后获取值。
-(void)fetchAllUserWithReference:(NSDictionary *)param
withCompletionHandler:(void (^)(NSDictionary *response))block{
FIRDatabaseReference *userReference = [[[FIRDatabase database] reference] child:RTDUSER];
[userReference observeEventType:FIRDataEventTypeChildAdded withBlock:^(FIRDataSnapshot * _Nonnull snapshot) {
NSDictionary *dict = snapshot.value;
block(dict);
} withCancelBlock:
block(nil);];
}
-(void)retrieveUsers {
NSDictionary *dict = [NSDictionary init];
[self fetchAllUserWithReference:dict withCompletionHandler:^(NSDictionary *response) {
NSLog(@"%@",response);
}];
}