Get distinct entity objects from NSMutableArray using NSPredicate in iPhone sdk [duplicate] - ios

This question already has answers here:
Removing duplicates from NSMutableArray
(9 answers)
Closed 10 years ago.
I have an NSMutableArray which has entity class object as its objects.
Now I want to remove the distinct objects from it. Consider following example
Entity *entity = [[Entity alloc] init];
entity.iUserId = 1;
entity.iUserName = #"Giri"
[arr addObject:entity];
Entity *entity = [[Entity alloc] init];
entity.iUserId = 2;
entity.iUserName = #"Sunil"
[arr addObject:entity];
Entity *entity = [[Entity alloc] init];
entity.iUserId = 3;
entity.iUserName = #"Giri"
[arr addObject:entity];
Now I want only two objects in the Array by removing the duplicate iUserName. I know the way by iteration but I want it without iterating it like predicate or some other way.
If anyone knows then please help me.
I had tried using [arr valueForKeyPath:#"distinctUnionOfObjects.iUsername"]; but it does not return me the entired object.
This question is totally different than the questions which are asked previously. Previously asked question is for getting the distinct objects is correct but they uses looping & I don't want this. I want it from NSPredicate or any other simple option which avoids looping.

EDIT: You can't do what you want to without looping over the array manually and building up a new array. The answer below won't work because it assumes that there are only at most two duplicates.
NSMutableArray *filteredArray = [NSMutableArray array];
for (Entity *entity in arr)
{
BOOL hasDuplicate = [[filteredArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"iUserName == %#", entity.iUserName]] count] > 0;
if (!hasDuplicate)
{
[filteredArray addObject:entity];
}
}
This will look for duplicates in the filtered array as it builds it.
Begin Original Answer
You can't use an NSSet because the Entity instances would have to return NSOrderedSame in compareTo:, which isn't a good idea since you shouldn't use names as unique identifiers.
You can use predicates, but they'll still loop over the array in an O(n^2) time without some optimization.
NSArray *filteredArray = [arr filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(Entity *evaluatedObject, NSDictionary *bindings) {
return [[arr filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"iUserName == %#", evaluatedObject.iUserName]] count] > 1;
}]];
That'll work fine. You could make it even faster by sorting the array by the iUserName property first and doing a linear scan over the sorted array (stopping when you see the first duplicate). That's a lot of work if you're dealing with small sample sizes (say, under ten thousand or so). It's probably not worth your time, so just use the code above.

Well you have a few options (that I can think of).
Use a NSSet instead of a NSArray.
Use a for loop (but you don't want to iterate through the array)
Use a predicate search iUserName to see if the name exists before adding it to the array.
Something like:
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"iUserName == 'Giri'"];
NSArray *searchArray = [arr filterUsingPredicate:predicate];

Related

ios nsarray/nsmutablearray group by/sum on custom objects

I'm just getting through tons of research and tutorials on ios app development.
Making my first app now and using array with tableviews. My question is now that I have populated arrays with custom objects, I want to query it.
Group By with Sum... things of that nature.
In my research I've found predicates for some types of filtering. So far I have successfully returned a new array where on property in my class is equal to "x".
Predicate worked for that part.
But I'm not sure how to expand to groupby/sum. I've been reading core data might accomplish this but is probably overkill.
Can someone please help me out with some options to research?
Thanks!!!
example
Person Class
name age salary
how can I group by name, and at the same time sum up the salaries?
I found this also ... is this an efficient way to tackle problem
// Get all the airline names with no duplicates using the KVC #distinctUnionOfObjects collection operator
NSArray *airlineNames = [arrayMealRating valueForKeyPath:#"#distinctUnionOfObjects.Airline"];
// Loop through all the airlines
for (NSString *airline in airlineNames) {
// Get an array of all the dictionaries for the current airline
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"(Airline == %#)", airline];
NSArray *airlineMealRating = [arrayMealRating filteredArrayUsingPredicate:predicate];
// Get the sum of all the ratings using KVC #sum collection operator
NSNumber *rating = [airlineMealRating valueForKeyPath:#"#sum.Rating"];
NSLog(#"%#: %#", airline, rating);
}
You probably want to use a dictionary so you can efficiently look up the running total for each person by name. Since you're new at Objective-C, I'll spell everything out:
NSMutableDictionary *totalSalaryForName = [NSMutableDictionary dictionary];
for (Person *person in people) {
NSString *name = [person name];
int total = [person salary];
NSNumber *priorTotal = [totalSalaryForName objectAtIndex:name];
if (priorTotal != nil) {
total += [priorTotal intValue];
}
NSNumber *newTotal = [NSNumber numberWithInt:total];
[totalSalaryForName setObject:newTotal forKey:name];
}
However, modern Objective-C provides some “syntactic sugar” you can use to shorten the code:
NSMutableDictionary *totalSalaryForName = [NSMutableDictionary dictionary];
for (Person *person in people) {
NSString *name = person.name;
int total = person.salary;
NSNumber *priorTotal = totalSalaryForName[name];
if (priorTotal != nil) {
total += priorTotal.intValue;
}
totalSalaryForName[name] = #(total);
}
Now you have a dictionary that maps each person's name to total salary.

nsdictionary with arrays nspredicate filtering

I have a tableview that i want to search through with a searchable. It worked before but when i added sections i got into trouble because i had to change from arrays to dictionary.
So basically i have a NSDictionary that looks like this
{ #"districtA": array with point objects, #"districtB": array with point objects}
I need to filter them based on the point objects.name that is in the arrays. After that i want to create a new nsdictionary with the filtered objects in it.
I tried at least 10 different methods but i can't figure it out so i think this is the only way that i am most positive that should work.
This is the only way i can think of if there is an easier way or more logic way please tell me.
-(void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope {
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"SELF.name BEGINSWITH[c] %#",searchText];
//create new array to fill
NSArray *arrayWithFilteredPoints = [[NSArray alloc] init];
//loop through the values and put into an rray based on the predicate
arrayWithFilteredPoints = [NSArray arrayWithObject:[[self.PointList allValues] filteredArrayUsingPredicate:predicate]];
NSMutableDictionary *dict = [#{} mutableCopy];
for (Point *point in arrayWithFilteredPoints) {
if (![dict objectForKey:Point.district])
dict[Point.district] = [#[] mutableCopy];
[dict[Point.district]addObject:Point];
}
self.filteredPointList = dict;
self.filteredDistrictSectionNames = [[dict allKeys] sortedArrayUsingSelector:#selector(localizedCaseInsensitiveCompare:)];}
This results in a crash, it happens of course where the predicate is used but i don't know how to debug what predicate i should use:
on 'NSInvalidArgumentException', reason: 'Can't do a substring operation with something that isn't a string (lhs = (
West ) rhs = w)'
I have read the comments and you are right. There was something wrong with my code.
I changed the logic, i added some more steps (like creating NSArray without needing it) to make the solution clear
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"SELF.name BEGINSWITH[c] %#",searchText];
//1. create new array to fill only the Points from the dictionary
NSArray *allPoints = [self.PointList allValues];
NSMutableArray *allPointObjects = [[NSMutableArray alloc]init];
for (NSArray *array in allPoints) {
for (Point *point in array) {
[allPointObjects addObject:point];
}
}
//2. loop through allPointObjects and put into an mutablearray based on the predicate
NSArray *arrayWithFilteredPoints = [[NSArray alloc] init];
arrayWithFilteredPoints = [allPointObjects filteredArrayUsingPredicate:predicate];
NSMutableDictionary *dict = [#{} mutableCopy];
for (Point *point in arrayWithFilteredPoints) {
if (![dict objectForKey:point.district])
dict[point.district] = [#[] mutableCopy];
[dict[point.district]addObject:Point];
}
self.filteredPointList = dict;
self.filteredDistrictSectionNames = [[dict allKeys] sortedArrayUsingSelector:#selector(localizedCaseInsensitiveCompare:)];
I wanted a filtered nsdictionary at the end that i can pass back to my tableview that reads the dictionary objects based on the keys (districts)
It seems clear from your description that [self.PointList allValues] is not an array of Point objects but an array of arrays of Point objects. That is the source of your difficulty, including your original crash.
You need to decide what to do about that; for example, if you want just one big array of Point objects, then flatten the array of arrays before you filter. I can't advise you further because it is not obvious to me what ultimate outcome you desire.
EDIT You've now modified your code and I can see more clearly what you're trying to do. You have a dictionary whose values are arrays of points, and you are trying to filter some of the points out of each array. What I would have done is to do that - i.e., run thru the keys, extract each array, filter it, and put it back (or delete the key if the array is now empty). But I can see that what you are doing should work, because you have cleverly put the keys into the points to start with, so you can reconstruct the dictionary structure from that.

Compare objects in two Collections

I have the following scenario:
I have two collection classes (could be NSArray, NSMutableArray, NSSet, NSOrderedSet or whatever would be best suited for this case), which hold unique objects of the same type (unique in the sense that for all objects in the collections for no two elements the isEqual method would return true).
Lets say the first collection instance holds the following objects (1,2,3,4,5) and the second one (2,3,4,6,7). Now I need a method that returns the difference between the two collections, with the extra info what exactly the difference from each collection was.
An example result for the example would be: (1,5) was removed from the first collection and (6,7) added two the second collection.
I know if I use the NSMutableArray with a sorted list and decide which list has more elements than the other, I could use removeObjectsInArray to get a list of the different objects (like described in Compare two arrays with the same value but with a different order or in How to compare and remove common objects( NSDictionaries) from 2 NSMutableArray?), but don't really know which objects was in which collection. I could create a temporary collection and put the result of removeObjectsInArray in that array and compare the other two initial arrays with the temporary array. Seems little verbose though. Is there a better way that I don't know of?
I found a much slicker way for you to do what you want by using NSPredicate. When I run the following code:
NSArray *firstArray = [[NSArray alloc] initWithObjects:#"1",#"2",#"3",#"4",#"5",#"6",#"7", nil];
NSArray *secondArray = [[NSArray alloc] initWithObjects:#"1",#"2",#"3",#"8",nil];
NSArray *itemsMissingFromSecondArray = [firstArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"NOT SELF IN %#", secondArray]];
NSArray *itemsMissingFromFirstArray = [secondArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"NOT SELF IN %#", firstArray]];
NSLog(#"itemsMissingFromFirstArray=%#\nitemsMissingFromSecondArray=%#", itemsMissingFromFirstArray, itemsMissingFromSecondArray);
I get the following output showing what was missing from each array that was in the other array:
itemsMissingFromFirstArray=(
8
)
itemsMissingFromSecondArray=(
4,
5,
6,
7
)
Less code than sorting and merging, doesn't use a bunch of temporary arrays, and simple enough to read.
NOTE: If someone also wants to know the items that are in both arrays, the solution is similarly simple:
NSArray *itemsFoundInBothArrays = [firstArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"SELF IN %#", secondArray]];
I think if you want to know the difference, you can make use of NSMutableSet and the minusSet function.
[set1 minusSet:set2];
will give you the elements in set1 but not in set2 straight away. So you don't need any temp collection and compare with original collection again.
Otherwise, if you only want to remove the elements, you can make use of NSArray and do sth like:
[secondArray removeObjectsInArray:firstArray];
Edited:
To find all the diff in one shot:
[ [set1 unionSet:set2] minusSet: [set1 intersectSet:set2] ];
+ (NSArray *) removeObjectsFromArray :(NSArray *)arrayToRemoveFrom thatAreAlsoIn:(NSArray *)arrayOfItemsToRemove
{
NSMutableArray *newArray = [[NSMutableArray alloc] initWithArray:arrayToRemoveFrom];
[newArray removeObjectsInArray:arrayOfItemsToRemove];
return newArray;
}
+(void) findArrayDifferences
{
NSArray *bigArray = [[NSArray alloc] initWithObjects:#"1",#"2",#"3",#"4",#"5",#"6",#"7", nil];
NSArray *smallArray = [[NSArray alloc] initWithObjects:#"1",#"2",#"3",#"8",nil];
NSArray *itemsInBigArrayThatAreNotInSmallArray = [self removeObjectsFromArray:bigArray thatAreAlsoIn:smallArray];
NSArray *itemsThatAreInBothArrays = [self removeObjectsFromArray:bigArray thatAreAlsoIn:itemsInBigArrayThatAreNotInSmallArray];
NSArray *itemsInSmallArrayThatAreNotInBigArray = [self removeObjectsFromArray:smallArray thatAreAlsoIn:itemsThatAreInBothArrays];
NSLog(#"itemsInBigArrayThatAreNotInSmallArray=%#\nitemsThatAreInBothArrays=%#\nitemsInSmallArrayThatAreNotInBigArray=%#", itemsInBigArrayThatAreNotInSmallArray, itemsThatAreInBothArrays, itemsInSmallArrayThatAreNotInBigArray);
}
This results in the following output:
itemsInBigArrayThatAreNotInSmallArray=(
4,
5,
6,
7
)
itemsThatAreInBothArrays=(
1,
2,
3
)
itemsInSmallArrayThatAreNotInBigArray=(
8
)

Code optimization (combine 2 lines of code into 1)

I have tried some combinations but can't seem to get it right. I am looking to combine line 2 and 3 into one line of code.
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"name == 'SSM M51 Copperhead'"];
NSArray *searchResults1 = [self.weaponsArray filteredArrayUsingPredicate:predicate];
weapons = [searchResults1 objectAtIndex:0];
if(weapons.range > SSMrange)
SSMrange = weapons.range;
('weapons' is a class).
weapons = [self.weaponsArray filteredArrayUsingPredicate:predicate][0];
This isn't really going to make your code any faster though.
How about defining a method like:
- (Weapon*)firstWeaponMatchingPredicateWithFormat:(NSString*)format
{
NSPredicate *predicate = [NSPredicate predicateWithFormat:format];
NSArray *searchResults1 = [self.weaponsArray filteredArrayUsingPredicate:predicate];
return (searchResults1.count > 0 ? [searchResults1 objectAtIndex:0] : nil);
}
Call with:
Weapon *weapon = [self firstWeaponMatchingPredicateWithFormat:#"name == 'SSM M51 Copperhead'"];
weapon = [[self.weaponsArray filteredArrayUsingPredicate:predicate] objectAtIndex:0];
There is no need to create an array object with the filtered results only to get the first object and throw away the collection.
If you're targeting iOS 4.0 or later, NSArray provides functionality to get the index of the first object passing a test through a block.
NSUInteger indexOfObject = [self.weaponsArray indexOfObjectPassingTest:^(id obj, NSUInteger index, BOOL *stop) {
return [[obj valueForKey:#"name"] isEqualToString:#"SSM M51 Copperhead"];
}];
The block will be executed once for each object and the array will stop processing when the block returns YES. Once you know the index of the object, you can get the value directly from you self.weaponsArray array. Just make sure you check the return value for NSNotFound.
I tried filtering an array of 1,000,000 objects and searching for the both the first and last object using the two approaches. Even when looking for the last object, the block approach was still quicker than using a predicate. I'm guessing due to the saving of not creating a filtered array.

Core Data Where clause between two tables?

I have two tables that I would like to get data and from by seeing if they match a particular attribute.
I want to write a predicate or something like this but I do not seem to be able to come up with a solution: #"Data.item == Item.code".
The reason why I don't use relationships is because my database was imported from mysql. So all the data is coming from outside of the app its being synced from downloaded mysql tables.
---------------------------EDIT-------------------------
What I have tried so far lots of things here is the crappy way I am doing this now perhaps from this you can understand more of what I am trying to do .
NSPredicate * Newpredicate = [NSPredicate predicateWithFormat:#"hid == 2"];
NSArray *row2 = [db DLook:Newpredicate table:#"Data"];
for (NSManagedObject *data in row2) {
NSLog(#"\n\n\n\nid\n\n\n\n: %#", [data valueForKey:#"id"]);
NSString *itemToCode = [data valueForKey:#"item"];
NSPredicate *itemPredicate = [NSPredicate predicateWithFormat:#"code == %#",itemToCode];
NSArray *itemRow = [db DLook:itemPredicate table:#"Item"];
for (NSManagedObject *item in itemRow) {
NSLog(#"\n\n\n\ncode : %#\n\n\n\n",[item valueForKey:#"code"]);
}
// NSLog(#"id: %#", [data valueForKey:#"id"]);
//NSManagedObject * itemhid= [data valueForKey:#"testRel"];
//NSLog(#"code: %#",[itemhid valueForKey:#"code"]);
}
NSLog(#"\n\n\n\n%d\n\n\n\n",[row2 count]);
The DLook is a convince method that just fetches the data using the predicate on the table that I pass. Then take the returned area of NSmanaged objects looping through them.
I wish I could just make a magical relationship that would let me get a all the Item.data that match the Data.items!!!
I don't want to do it like this I want to make a relationship that would work like that.
Help
Thanks
Your equality in the predicate would only be true if the two objects are actually the same object. You could do this:
[NSPredicate predicateWithFormat:#"item = %#", code];
However, you should really try to get the relationships right when importing your model. Then you would not even have to do a fetch from the store but just use
dataObject.item;
So, when you import do something like this:
// get one data object from your source
// get the corresponding item object from your source
Data *dataObject = [NSEntityDescription insertNewObjectForEntityForName:#"Data"
inManagedObjectContext:self.managedObjectContext];
Item *itemObject = [NSEntityDescription insertNewObjectForEntityForName:#"Item"
inManagedObjectContext:self.managedObjectContext];
[dataObject addItemObject:itemObject];

Resources