在两个 class 之间使用 NSDictionary 值

Use NSDictionary values between two class

我正在尝试获取两个class之间的NSDictionary内的值..

我在Database.m(NSObjectclass)

中使用了这个方法
+(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) class 中获取 NSDictionary 数据 ..

-(void)retrieveUsers {
    NSDictionary *dict = [[NSDictionary alloc] init];
    [Database fetchAllUserWithReference:dict];    
    NSLog(@"DICT %@",dict);
}

为什么我的 class Dashboard.mNSDictionary 总是 NULL ??我哪里错了?

您可以在执行块后使用完成处理程序取回值。

-(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);
    }];
}