Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Too slow #77

Closed
chiragpurohit71085 opened this issue Oct 24, 2018 · 2 comments
Closed

Too slow #77

chiragpurohit71085 opened this issue Oct 24, 2018 · 2 comments

Comments

@chiragpurohit71085
Copy link

I had implemented FBLPromiseAwait for firebase. It basically gets some data from firebase node and iterate for various condition. This takes around 15 seconds for 605 records, while iteration with normal firebase functions takes only 2 to 3 seconds i.e

[[[[[[FIRDatabase database] reference] child:USERS_TABLE] queryOrderedByChild:@"user_id"] queryEqualToValue:[[obj objectForKey:@"data"] objectForKey:@"user_id"]] observeSingleEventOfType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot * _Nonnull snapshot){ ..........

Please help me if I am implementing anything wrong

here is my sample code FBLPromiseAwait.

` FBLPromise<NSMutableArray *> *promise = [FBLPromise
onQueue:dispatch_queue_create(NULL, DISPATCH_QUEUE_SERIAL)
do:^id {
NSError *error;
NSDictionary *notifications = FBLPromiseAwait([self getSomeData], &error);

                                             NSArray*notification_keys = [notifications allKeys];
                                             
                                             for(int notification_count=0;notification_count<[notification_keys count ];notification_count++)
                                            
                                             {
                                                 
                                                 NSDictionary*obj = [notifications objectForKey:[notification_keys objectAtIndex:notification_count]];
                                                 
                                                 
                                                 
                                                
                                                 
                                                 NSString *notifType = [[obj objectForKey:@"data"] objectForKey:@"type"];
                                                 NSString *timeString = [[PBDateFormatter sharedFormatter] getCustomDateStringForChat:[NSDate dateWithTimeIntervalSince1970:[[[obj objectForKey:@"data"] objectForKey:@"timestamp"] longLongValue]/1000]];
                                                 NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:[[obj objectForKey:@"notification"] objectForKey:@"title"] attributes:@{NSFontAttributeName : [UIFont fontWithName:PINGBUDS_FONT_NORMAL size:12], NSForegroundColorAttributeName : [UIColor blackColor]}];
                                                 
                                                 
                                                 //SOMEREFERENCE,conversation/deleteByInitiatingUser
                                                 if([notifType isEqualToString:@"SOMEREFERENCE"] || [notifType isEqualToString:@"conversation/deleteByInitiatingUser"] )
                                                 {
                                                     
                                                     NSDictionary*userData = FBLPromiseAwait(
                                                                                             [FBLPromise async:^(FBLPromiseFulfillBlock fulfill, FBLPromiseRejectBlock __unused _) {
                                                         
                                                         
                                                         [[[[[[FIRDatabase database] reference] child:USERS_TABLE] queryOrderedByChild:@"user_id"] queryEqualToValue:[[obj objectForKey:@"data"] objectForKey:@"user_id"]] observeSingleEventOfType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot * _Nonnull snapshot){
                                                             
                                                             fulfill([[snapshot.value allObjects] firstObject]);
                                                             
                                                         }];
                                                         
                                                         
                                                     }], &error);
                                                     NSLog(@"\n promise user userData - %@",userData);
                                                     
                                                     
                                                     
                                                     
                                                     NSDictionary*skillFound = FBLPromiseAwait(
                                                                                               [FBLPromise async:^(FBLPromiseFulfillBlock fulfill, FBLPromiseRejectBlock __unused _) {
                                                         
                                                         [[[[[[FIRDatabase database] reference] child:SOME_TABLE] queryOrderedByChild:@"id"] queryEqualToValue:[[obj objectForKey:@"data"] objectForKey:@"skill_id"]] observeSingleEventOfType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot * _Nonnull snapshot){
                                                             
                                                             
                                                             NSLog(@"\n\n obj at moment - %@",obj);
                                                             
                                                             fulfill([[snapshot.value allObjects] firstObject]);
                                                             
                                                             
                                                         }];
                                                     }], &error);
                                                     
                                                     
                                                    [_dataSource addObject:model];
                                                     
                                                     
                                                     
                                                     //Ends : SOMEREFERENCE,conversation/deleteByInitiatingUser
                                                     
                                                 }
                                                 
                                               
                                            
                                                 else if([components[0] isEqualToString:@"XXX"]){}
                                                 else if([notifType isEqualToString:@"YYYY"]){}
                                                 else if([notifType isEqualToString:@"zzzz"]){}
                                                 else if([notifType isEqualToString:@"pppp"]){}
                                                 
                                                 
                                                 
                                                 
                                             }
                                             
                                            
                                             dispatch_async(dispatch_get_main_queue(), ^{
                                                 
                                                 [MBProgressHUD hideHUDForView:self.view animated:YES];
                                                 [self do_some_ui_stuff];
                                                 
                                                 
                                                 
                                             });
                                             
                                             
                                             
                                             
                                             if (error) {
                                                 return error;
                                             }
                                             
                                             return _dataSource;
                                             
                                             
                                         }];`
@shoumikhin
Copy link
Contributor

Hi @chiragpurohit71085,

I doubt that you really need to use FBLPromiseAwait at all. Normally, you should be good by simply chaining promises into pipelines, possibly with the help of misc extensions operators.

Like for your example I imagine something akin the following:

[[[self getSomeData] then:^id(NSDictionary *notifications) {
  NSMutableArray<FBLPromise *> *modelPromises = [NSMutableArray new];
  for(id key in notifications) {
    NSDictionary *notification = notifications[key];
    NSString *notificationType = notification[@"data"][@"type"];

    if ([notificationType isEqualToString:@"SOMEREFERENCE"] ||
        [notificationType isEqualToString:@"conversation/deleteByInitiatingUser"]) {
      FBLPromise<NSDictionary *> *userDataPromise =
          [[FBLPromise wrapObjectCompletion:^(FBLPromiseObjectCompletion handler) {
            [[[[[[FIRDatabase database] reference] child:USERS_TABLE]
                queryOrderedByChild:@"user_id"]
                    queryEqualToValue:[[obj objectForKey:@"data"] objectForKey:@"user_id"]]
                        observeSingleEventOfType:FIRDataEventTypeValue
                                       withBlock:handler];
          }] then:^id(FIRDataSnapshot *snapshot) {
            return snapshot.value.allObjects.firstObject;   
          }];
      FBLPromise<NSDictionary *> *skillFoundPromise =
          [[FBLPromise wrapObjectCompletion:^(FBLPromiseObjectCompletion handler) {
            [[[[[[FIRDatabase database] reference] child:SOME_TABLE]
                queryOrderedByChild:@"id"]
                    queryEqualToValue:[[obj objectForKey:@"data"] objectForKey:@"skill_id"]]
                        observeSingleEventOfType:FIRDataEventTypeValue
                                       withBlock:handler];
          }] then:^id(FIRDataSnapshot *snapshot) {
            return snapshot.value.allObjects.firstObject;   
          }];
      FNLPromise *modelPromise =
          [[FBLPromise all:@[userDataPromise, skillFoundPromise]] then:^id(NSArray *values) {
            NSDictionary *userData = values.firstObject;
            NSDictionary *skillFound = values.lastObject;
            return [[Model alloc] initWithUserData:userData skillFound:skillFound];
          }];
      [modelPromises addObject:modelPromise];
    }
  }
  return [FBLPromise all:modelPromises];
}] then:^id(NSArray<Model *> *models) {
  [MBProgressHUD hideHUDForView:self.view animated:YES];
  [self do_some_ui_stuff]; 
  return nil;
}];

Which can be simplified and refactored further, if you move some of the patterns into separate methods, for example.

@chiragpurohit71085
Copy link
Author

chiragpurohit71085 commented Oct 25, 2018 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants