A user on my app can select filters to narrow down items. Once filters are selected, an NSNotification
is sent that passes an array of selected filters (NSString
s) to another view controller.
When I receive a notification, I can extract the array from [notification userInfo]
. Now, what I want to do is to compare the new filters with those already saved for the user, so that I make a call to the server only if they are different. Here's my method:
- (void)applyFilters:(NSNotification *)notification {
NSArray *filters = [[notification userInfo] objectForKey:@"filters"];
if (showMyItems) {
if (![[NSSet setWithArray:filters] isEqualToSet:[NSSet setWithArray:[[PFUser currentUser] objectForKey:@"filtersMy"]]]) {
[[PFUser currentUser] setObject:[NSArray arrayWithArray:filters] forKey:@"filtersMy"];
[[PFUser currentUser] saveInBackground];
}
} else {
if (![[NSSet setWithArray:filters] isEqualToSet:[NSSet setWithArray:[[PFUser currentUser] objectForKey:@"filters"]]]) {
[[PFUser currentUser] setObject:[NSArray arrayWithArray:filters] forKey:@"filters"];
[[PFUser currentUser] saveInBackground];
}
}
}
Here I'm comparing NSSet
s rather than NSArray
s because the order of objects/strings is not important. What I try to do then is create a new array and set is as user's new filters via arrayWithArray:
. So, when the next time the user changes filter selection, I expect that new filters
array will be different from the one saved for the user (my if statement condition). However, when I NSLog both (new and saved/fetched arrays), they are exactly the same! And therefore the if condition never passes.
My only explanation of this is that the [NSArray arrayWithArray:filters]
is somehow still pointing to filters
array.
I even tried the [NSKeyedUnarchiver unarchiveObjectWithData:[NSKeyedArchiver archivedDataWithRootObject:filters]]
suggested on SO and by Apple, but the result is the same.
Can somebody please spot my problem? I'm out of options and ideas...
UPDATE:
I have now pasted the full method (above). A bit more background:
- I am using two instances of the same
UIViewController
class - In one instance,
showMyItems
is set toYES
(so only the top part ofif
statement is called). In another instance -NO
, so only the lower part is called - What is interesting, when
showMyItems
isNO
, my code works as expected! But whenshowMyItems
isYES
, it doesn't... Although the code is identical.
Any thoughts?