【问题标题】:Retrieve data from firebase database and show in UITable in Objective-C从 firebase 数据库中检索数据并在 Objective-C 中的 UITable 中显示
【发布时间】:2017-01-26 07:52:30
【问题描述】:

我在 Firebase 中有以下数据库,

我正在尝试检索数据并在 UITable 视图中显示它们。首先,我想从数据库中检索协议。目前只有一个协议记录,但未来会有很多记录。我试过下面的代码

- (void)viewDidLoad {
    [super viewDidLoad];

    // Uncomment the following line to preserve selection between presentations.
    //self.clearsSelectionOnViewWillAppear = NO;

    [[_firebaseDatabaseRef child:@"krib-60229"] observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *_Nonnull snapshot){
        self.allSnapshot = [NSMutableArray array];
        NSLog(@"%@",self.allSnapshot);
        NSLog(@"%s","TestTestTest");
        for(snapshot in snapshot.children){
            [self.allSnapshot addObject:snapshot];
        }
        [self.tableView reloadData];
    }];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.allSnapshot count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    FIRDataSnapshot *snapShot = [self.allSnapshot objectAtIndex:indexPath.row];

    NSString *testText = snapShot.value[@"landlordDisplayName"];

    cell.textLabel.text = testText;

    // Configure the cell...

    return cell;
}

我也没有得到任何检索或任何错误。我尝试放置 NSLog,但它们也没有打印。

【问题讨论】:

    标签: ios objective-c uitableview firebase firebase-realtime-database


    【解决方案1】:

    请尝试以下几行。

     FIRDatabaseReference *rootRef= [[FIRDatabase database] referenceFromURL:[NSString stringWithFormat:@"%@/agreements",<database-url>]];
        [rootRef observeSingleEventOfType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot)
        {
            if (snapshot.exists)
            {
                NSLog(@“%@”,snapshot.value);            
            }
        }];
    

    【讨论】:

      【解决方案2】:

      @property FIRDatabaseReference *ref;

      "在上面声明这个 ref 属性"

      self.ref=[[FIRDatabase database] reference];
      [[_ref child:@"your_child_name"] observeEventType:FIRDataEventTypeChildAdded withBlock:^(FIRDataSnapshot * _Nonnull snapshot) {
          if (snapshot.exists) {
              NSMutableDictionary *agreement = snapshot.value;
              NSLOG(@"%@" , agreement)
          }
      
      }
       withCancelBlock:^(NSError * _Nonnull error) {
       NSLog(@"%@", error.localizedDescription);
      }];
      

      这对我有用!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-11-21
        • 2017-07-31
        • 1970-01-01
        相关资源
        最近更新 更多