Advertisement
Guest User

Untitled

a guest
May 19th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -(void)viewDidLoad{
  2.  
  3.     [self queryFifeScoresForTopUsers];
  4.  
  5. }
  6.  
  7. //Queries the Scores object in Firebase and sorts them based on the score key
  8. -(void)queryFifeScoresForTopUsers {
  9.    
  10.     [self.topUsersArray removeAllObjects];
  11.    
  12.     FIRDatabaseQuery *sortedRef = [[[[[FIRDatabase database] reference] child:@"Scores"] queryOrderedByChild:@"fifeScore"] queryLimitedToLast:5];
  13.    
  14.     [sortedRef observeSingleEventOfType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
  15.        
  16.         if(snapshot.exists){
  17.            
  18.             for(NSMutableDictionary *object in [snapshot.value allValues]){
  19.                
  20.                 [self.topUsersArray addObject:object];
  21.                
  22.                 [self loadTopUsersForFriendSuggestions];
  23.                
  24.                 NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"fifeScore" ascending:NO];
  25.                 NSArray *arrayOfDescriptors = [NSArray arrayWithObject:sortDescriptor];
  26.                
  27.                 [self.topUsersArray sortUsingDescriptors: arrayOfDescriptors];
  28.  
  29.             }
  30.            
  31.             [self setupUidArray];
  32.         }
  33.     }];
  34. }
  35.  
  36. //Adds the uid from each object in self.topUsersArray into a new array self.uidArray
  37. -(void)setupUidArray{
  38.    
  39.     for (NSMutableDictionary *object in self.topUsersArray){
  40.        
  41.         NSString *uid = [object objectForKey:@"uid"];
  42.        
  43.         if (uid){
  44.             [self.uidArray addObject:uid];
  45.         }
  46.     }
  47.    
  48. }
  49.  
  50. //Queries all users, loops through each object, adds object to self.suggestedUsersArray if they match
  51. -(void)loadTopUsersForFriendSuggestions{
  52.  
  53.     FIRDatabaseReference *userRef = [[[FIRDatabase database] reference] child:@"Users"];
  54.    
  55.     [userRef observeSingleEventOfType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot * _Nonnull snapshot) {
  56.         if (snapshot.exists){
  57.                
  58.             for (NSString *uid in [snapshot.value allKeys]){
  59.                
  60.                 if([self.uidArray containsObject:uid]) {
  61.                    
  62.                     [self.suggestedUsersArray addObject:[snapshot.value objectForKey:uid]];
  63.                     NSLog(@"%@",uid);
  64.                    
  65.                 }
  66.             }
  67.            
  68.         }
  69.     }];
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement