2

when I run the following code:


[[FIRAuth auth] signInWithEmail:@"myemail@mydomain.com"
                           password:@"hdfjhfjhdbf"
                         completion:^(FIRUser *user, NSError *error) {

                             NSLog(@"I am in Block");
                         }];
NSLog(@"I am here");

my output is : I am here I am in Block it means that the FIRAuth block runs in another thread other than main thread. I add an extra line to the code (while infinite loop)


[[FIRAuth auth] signInWithEmail:@"myemail@mydomain.com"
                           password:@"hdfjhfjhdbf"
                         completion:^(FIRUser *user, NSError *error) {

                             NSLog(@"I am in Block");
                         }];
NSLog(@"I am here");
while (YES) {};

my output is : I am here

the question: if the FIRAuth block runs in another thread it should print "I am in block" no matter we have infinite loop in main thread. how could it be possible?does the FIRAuth block runs by less priority in same main thread?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
EhsanR
  • 467
  • 7
  • 17

1 Answers1

2

Usually in Firebase we:

  • run the operations that interact with network and disk in a separate thread

  • surface the callbacks back on the main thread, so that your code can interact with the UI

I didn't verify for this specific case, but expect it to work the same. And that means that your infinite loop is preventing the completion block from being executed.

If you want to wait for a result, use semaphores as describes here: How do I wait for an asynchronously dispatched block to finish?

Community
  • 1
  • 1
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Does FIRAuth have a property that return "true" when the block is completed? I had experience with Dynamodb amazon, their functions have "completed" boolean property – EhsanR Jun 21 '16 at 03:21
  • It has a property `currentUser`, which is documented as "Synchronously gets the cached current user, or null if there is none." The documentation is your friend here: https://firebase.google.com/docs/reference/ios/firebaseauth/interface_f_i_r_auth#instance-method-summary – Frank van Puffelen Jun 21 '16 at 03:45
  • but I am still not completely convinced how FIRAuth threats the threads. the following example show that infinite loop does not block other threads executing. Are you aware of any dispatch setting that behaves like FIRAuth in which infinite loop blocks other queues execution? dispatch_queue_t queue=dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0); dispatch_async(queue, ^{ while (TRUE){NSLog(@"I'm in Block 1"); usleep(200);} }); dispatch_async(queue, ^{ while (TRUE){NSLog(@"I'm in Block 2"); usleep(200);} }); while (TRUE){}; – EhsanR Jun 21 '16 at 17:07