在两个类之间使用NSDictionary值

问题描述 投票:0回答:1

我试图在两个类之间的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);
}

为什么我的NSDictionaryDashboard.m总是为空?我哪里错了?

ios firebase-realtime-database nsdictionary nsobject
1个回答
1
投票

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

-(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);
    }];
}
© www.soinside.com 2019 - 2024. All rights reserved.