我将用户邮政编码保存到Firebase数据库,并希望在应用启动时查询该数据库,以查看用户是否已输入其邮政编码或是否为全新用户。
我以前发过我的代码。 我从Firebase文档中提取了示例代码,但似乎我的应用程序甚至从未运行以下代码来获取值
[[[_ref child:@"user"] child:userID] observeSingleEventOfType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot * _Nonnull snapshot) {...我错过了什么?
#import "FollowingVC.h" #import <FirebaseDatabase/FirebaseDatabase.h> @import Firebase; @interface FollowingVC () @property NSString *uid; @property FIRDatabaseReference *ref; @property NSString *zipcode; @end @implementation FollowingVC - (void)viewDidLoad { [super viewDidLoad]; [self createAuthorizedUser]; [self checkForZipCode]; } -(void)createAuthorizedUser { [[FIRAuth auth] signInAnonymouslyWithCompletion:^(FIRUser *_Nullable user, NSError *_Nullable error) { if (!error) { self.uid = user.uid; self.ref=[[FIRDatabase database]reference]; } }]; } -(void)checkForZipCode { NSString *userID = [FIRAuth auth].currentUser.uid; [[[_ref child:@"user"] child:userID] observeSingleEventOfType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot * _Nonnull snapshot) { // Get user value self.zipcode = snapshot.value[@"zip code"]; NSLog(@"It worked: %@", self.zipcode); // ... } withCancelBlock:^(NSError * _Nonnull error) { NSLog(@"%@", error.localizedDescription); }]; } @endI'm saving a users zip code to the Firebase database and want to query that database on app launch to see if the user has input their zip code already or if they're a brand new user.
I've posted my code before. I pulled the sample code from the Firebase docs, but it seems that my app is never even running the following code to get the value
[[[_ref child:@"user"] child:userID] observeSingleEventOfType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot * _Nonnull snapshot) {...What am I missing out on?
#import "FollowingVC.h" #import <FirebaseDatabase/FirebaseDatabase.h> @import Firebase; @interface FollowingVC () @property NSString *uid; @property FIRDatabaseReference *ref; @property NSString *zipcode; @end @implementation FollowingVC - (void)viewDidLoad { [super viewDidLoad]; [self createAuthorizedUser]; [self checkForZipCode]; } -(void)createAuthorizedUser { [[FIRAuth auth] signInAnonymouslyWithCompletion:^(FIRUser *_Nullable user, NSError *_Nullable error) { if (!error) { self.uid = user.uid; self.ref=[[FIRDatabase database]reference]; } }]; } -(void)checkForZipCode { NSString *userID = [FIRAuth auth].currentUser.uid; [[[_ref child:@"user"] child:userID] observeSingleEventOfType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot * _Nonnull snapshot) { // Get user value self.zipcode = snapshot.value[@"zip code"]; NSLog(@"It worked: %@", self.zipcode); // ... } withCancelBlock:^(NSError * _Nonnull error) { NSLog(@"%@", error.localizedDescription); }]; } @end最满意答案
Firebase是异步的,您需要留出时间让事件完成,然后才能继续使用该应用。
在这种情况下,您应该在填充self.uid之后在登录块内调用[self checkForZipCode]。
否则,在填充self.uid之前,您将面临运行checkForZipCode函数的风险。
让Firebase控制应用程序的流量 - 并且不要尝试同步使用Firebase,因为它会因为互联网滞后等而让您陷入困境。
Firebase is asynronous and you need to allow time for events to complete before moving on in the app.
In this case, you should call [self checkForZipCode] inside the sign-in block after self.uid is populated.
Otherwise you run the risk of the checkForZipCode function running before the self.uid is populated.
Let Firebase control the flow of the app - and don't try to use Firebase synchronously as it will get you into trouble due to internet lag etc.
更多推荐
发布评论