簡體   English   中英

如何使用Objective-C在Firebase中保存子值

[英]How to save child value in Firebase using Objective-C

我正在嘗試使用Firebase在我正在處理的應用中保存少量用戶數據。 我想保存用戶郵政編碼(他們將輸入),然后讓他們選擇關注某些主題。

我目前正在保存郵政編碼,但我無法正確保存子數據。

任何人都可以幫助指出我需要添加的內容,以允許從用戶郵政編碼中保存子數據嗎?

#import "ZipCodeVC.h"
#import <FirebaseStorage/FirebaseStorage.h>
#import <FirebaseDatabase/FirebaseDatabase.h>

@interface ZipCodeVC ()

@property FIRDatabaseReference *ref;

@end

@implementation ZipCodeVC

- (void)viewDidLoad {
    [super viewDidLoad];

    self.ref=[[FIRDatabase database]reference];
    [[self.ref child:@"Zip Code"] setValue:@"61354"];

}


@end

弄清楚了!

#import "ZipCodeVC.h"
#import <FirebaseStorage/FirebaseStorage.h>
#import <FirebaseDatabase/FirebaseDatabase.h>
@import Firebase;

@interface ZipCodeVC ()

@property NSString *uid;
@property (strong, nonatomic) IBOutlet UITextField *zipcodeTextField;

@property FIRDatabaseReference *ref;

@end

@implementation ZipCodeVC

- (void)viewDidLoad {
    [super viewDidLoad];

}

- (IBAction)tappedSubmit:(id)sender
{
    [[FIRAuth auth]
     signInAnonymouslyWithCompletion:^(FIRUser *_Nullable user, NSError *_Nullable error) {
         if (!error) {
             self.uid = user.uid;

             self.ref=[[FIRDatabase database]reference];
             //[[self.ref child:@"User ID"] setValue:uid]; old code that worked for creating main value

             [self writeNewPost:self.uid zipcode:self.zipcodeTextField.text title:@"Test Title" body:@"Body"];

         }
     }];
}


- (void)writeNewPost:(NSString *)userID zipcode:(NSString *)zipcode title:(NSString *)title body:(NSString *)body
{
    NSString *key = self.uid;

    NSDictionary *post = @{@"uid": userID,
                           @"zip code": zipcode,
                           @"title": title,
                           @"body": body};
    NSDictionary *childUpdates = @{[NSString stringWithFormat:@"/user/%@", key]: post};

    [_ref updateChildValues:childUpdates];
}


@end

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM