Use NSPredicate to match identifier in two arrays

Hi, I have maybe a simple question, but I cannot handle it.

  • I have a "Location" of Modelclass class which contains an array with category identifier (12, 23, 56).
  • Then I have an array with all the available category identifiers (1,2,3,4,5,6,7, ...)
  • All of these categories have IDs that are displayed in the TableView and can choose or not.
  • I show the marker hit on MapView (the annotation class is the location), which should be displayed based on the selection of filters in the mentioned TableView filter.

I need to implement the "Filter by category" function, which removes all the markers and adds them again, but only based on the selection in the list.

So, I need to compare the array with the filter identifier in the location model with the array with the entire filter identifier in the TableView. For this, I used the following function:

for (Location *currLocation in arrListOfLocationsNearby) { 

for (NSString * currKatId in currLocation.arrKatIds) {

 NSInteger intCurrKatId = [currKatId integerValue]; NSLog(@"Current KatId %@ id: %d \n", currLocation.strAdr, intCurrKatId); for (Filter *currFilter in [SDM getSharedDataManager].arrListOfFilterOptions) { if (currFilter.isSelected) { NSInteger intCurrFilterId = [currFilter.strAtt_id intValue]; NSLog(@"Current Filter %@ id: %d \n", currFilter.strAtt_text, intCurrFilterId); if ([currKatId intValue] == [currFilter.strAtt_id intValue]) { currLocation.isVisible = YES; }else { currLocation.isVisible = NO; } } } 

}}

I know that this will be the most inefficient way to get around everything. I want to somehow use NSPredicate for this, but I have never used them before, and I can not find examples for my problem.

Any clues from you guys?

refers to m.

+4
source share
3 answers

So, each Location object has many “category identifiers”? And do you want to show any Location objects that have a category identifier inside your " arrayOfEnabledCategoryIDs "?

Assuming you have a Location object and a CategoryID object, and there is a many-to-many relationship ( locations <<--->> categories ) between them, you can do this:

 NSArray * arrayOfEnabledCategoryIDs = ...; //these are the categories to display NSFetchRequest * f = [[NSFetchRequest alloc] init]; [f setEntity:theEntityDescriptionForLocationObjects]; [f setPredicate:[NSPredicate predicateWithFormat:@"ANY categories.categoryID IN %@", arrayOfEnabledCategoryIDs]]; 
+6
source

Try to have a string representation of each test:

 NSArray *arrayOfPredicateStrings = ;// array of all tests, probably [SDM sharedDataManager].arrListOfFilterOptions NSPredicate *enabled = [NSPredicate predicateWithFormat:@"isSelected == true"]; NSArray *arrayOfEnabledPredicateStrings = [arrayOfPredicateStrings filteredArrayUsingPredicate:enabled]; NSMutableString *formatString; BOOL firstTime = YES; for (NSString *string in arrayOfEnabledPredicateStrings) { // Concatinate strings with OR inbetween if (firstTime) { firstTime = NO; formatString = [string mutableCopy]; } else { [formatString appendFormat:@" OR %@", string]; } } // formatString should look something like @"ivar1 >= 1 OR ivar2 == true OR ivar3 != \"Hello, World\"" NSPredicate *predicate = [NSPredicate predicateWithFormat:string]; NSArray *arrListOfLocationsNearby; // assume already filled NSArray *suitableNearbyLocations = [arrListOfLocationsNearby filteredArrayUsingPrediacate:predicate]; 

If the CoreData datastore is behind your SDM class, simply create a fetch request as follows:

 NSPredicate *predicate = ;// get the predicate as above NSManagedObjectContext *context = ;// a managed object context NSEntityDescription *entity = [NSEntityDescription entityForName:@"entityName" inManagedObjectContext:context]; NSFetchRequest *request = [[NSFetchRequest alloc] init]; request.entity = entity; request.predicate = predicate; NSError *error; NSArray *results = [context executeFetchRequest:request error:&error]; [request release]; 

The predicate format syntax can be found here and usage can be found here.

0
source

If you are familiar with Ruby On Rails ActiveRecord. I am using a large library called activerecord + fetching + for + core, which you can find here here http://github.com/magicalpanda/activerecord-fetching-for-core-data

 NSArray *departments = [NSArray arrayWithObjects:dept1, dept2, ..., nil]; NSPredicate *peopleFilter = [NSPredicate predicateWithFormat:@"Department IN %@", departments]; NSArray *people = [Person findAllWithPredicate:peopleFilter]; 

He really cleared my code.

0
source

Source: https://habr.com/ru/post/1313611/


All Articles