Search code examples
iosnspredicatensset

ios - Subtract NSSet from other NSSet


I have two NSSets. One contains all UserKeys and other contains online user keys. I want to show in two sections this values. Section 1- online users and section2 - other users

I did this using predicate like below.

NSSet *nsset1 = [NSSet setWithArray:attendeesService.arrof_attendeeList]; //this are all users
nsset2 = [NSSet setWithArray:tempArray]; // temparray contains all online users.
NSSet *nsset2_ids = [nsset2 valueForKey:@"UserProfileKEY"]; 
nsset1_minus_nsset2 = [nsset1 filteredSetUsingPredicate:[NSPredicate predicateWithFormat:@"NOT UserProfileKEY IN %@",nsset2_ids]]; 

It works fine. But All users are about 3000+ and online may be 200+ at a time. So it is taking a lot of time. It takes 1 min for 4-5 online users. So for 200+ online users will take more time. Is there a faster solution for this?

Thanks.


Solution

  • Your First Array is FirstMakes declaration is :

    NSArray *FirstMakes = @[@"Mercedes-Benz", @"BMW", @"Porsche",
                             @"Opel", @"Volkswagen", @"Audi"];
    

    Your second Array is SecondMakes declaration is :

    NSArray *SecondMakes = @[@"Mercedes-Benz", @"Porsche",
                              @"Volkswagen"];
    
    Filter `FirstMakes` to `SecondMakes`
    
    NSPredicate *beforeL = [NSPredicate predicateWithBlock:
                                ^BOOL(id evaluatedObject, NSDictionary *bindings)
                                 {
    
                                     if ([SecondMakes containsObject:evaluatedObject])
                                     {
                                         return NO;
                                     }
                                     else{
                                         return YES;
                                     }
    
                                }];
        NSArray *makesBeforeL = [FirstMakes
                                 filteredArrayUsingPredicate:beforeL];
    
        NSLog(@"%@", makesBeforeL);
    

    Your OUTPUT is :

    enter image description here