Last active
December 12, 2015 01:59
-
-
Save emcmanus/4695605 to your computer and use it in GitHub Desktop.
Objective-C isEqual implementation for Parse objects (PFObject, PFFile, and PFRelation)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// PFObject+YSComparison.h | |
// | |
// Created by Ed McManus for Yardsale Inc. on 2/1/13 | |
// Learn more at https://getyardsale.com | |
// | |
// Requires BlocksKit https://github.com/pandamonia/BlocksKit | |
// | |
// Permission is hereby granted, free of charge, to any person obtaining a copy | |
// of this software and associated documentation files (the "Software"), to deal | |
// in the Software without restriction, including without limitation the rights | |
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
// copies of the Software, and to permit persons to whom the Software is | |
// furnished to do so, subject to the following conditions: | |
// | |
// The above copyright notice and this permission notice shall be included in | |
// all copies or substantial portions of the Software. | |
// | |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
// THE SOFTWARE. | |
// | |
#import <Parse/Parse.h> | |
@interface PFObject (YSComparison) | |
- (NSDictionary*)ys_dictionaryRepresentation; | |
- (BOOL)ys_isEqual:(id)object; | |
@end | |
@interface PFFile (YSComparison) | |
- (BOOL)ys_isEqual:(id)object; | |
@end | |
@interface PFRelation (YSComparison) | |
- (BOOL)ys_isEqual:(id)object; | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// PFObject+YSComparison.m | |
// | |
// Created by Ed McManus for Yardsale Inc. on 2/1/13 | |
// Learn more at https://getyardsale.com | |
// | |
// Requires BlocksKit https://github.com/pandamonia/BlocksKit | |
// | |
// Permission is hereby granted, free of charge, to any person obtaining a copy | |
// of this software and associated documentation files (the "Software"), to deal | |
// in the Software without restriction, including without limitation the rights | |
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
// copies of the Software, and to permit persons to whom the Software is | |
// furnished to do so, subject to the following conditions: | |
// | |
// The above copyright notice and this permission notice shall be included in | |
// all copies or substantial portions of the Software. | |
// | |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
// THE SOFTWARE. | |
// | |
#import "PFObject+YSComparison.h" | |
static bool compareBySelectors(SEL selectors[], NSObject* object1, NSObject* object2); | |
#pragma mark - PFFile | |
@implementation PFFile (YSComparison) | |
- (BOOL)ys_isEqual:(id)object | |
{ | |
if ([self isKindOfClass:[object class]]) | |
{ | |
// Skip getData check if it requires a request | |
if (![self isDataAvailable] || ![object isDataAvailable]) | |
{ | |
SEL selectors[] = { @selector(url), @selector(name), @selector(isDataAvailable), }; | |
return compareBySelectors( selectors, self, object ); | |
} | |
else | |
{ | |
SEL selectors[] = { @selector(url), @selector(name), @selector(isDataAvailable), @selector(getData), }; | |
return compareBySelectors( selectors, self, object ); | |
} | |
} | |
return NO; | |
} | |
@end | |
#pragma mark - PFRelation | |
@implementation PFRelation (YSComparison) | |
- (BOOL)ys_isEqual:(id)object | |
{ | |
if ([object isKindOfClass:[self class]]) | |
{ | |
SEL selectors[] = { | |
@selector(parent), | |
@selector(key), | |
@selector(targetClass), | |
}; | |
return compareBySelectors(selectors, self, object); | |
} | |
return NO; | |
} | |
@end | |
#pragma mark - PFObject | |
@implementation PFObject (YSComparison) | |
- (NSDictionary*)ys_dictionaryRepresentation | |
{ | |
NSArray * keys = [self allKeys]; | |
NSArray * values = [keys map:^id(NSString *key) { | |
return self[key]; | |
}]; | |
return [NSDictionary dictionaryWithObjects:values forKeys:keys]; | |
} | |
- (BOOL)ys_isEqual:(id)object | |
{ | |
if ([object isKindOfClass:[self class]]) | |
{ | |
SEL selectors[] = { | |
@selector(objectId), | |
@selector(createdAt), | |
@selector(updatedAt), | |
@selector(className), | |
@selector(ys_dictionaryRepresentation), | |
}; | |
return compareBySelectors(selectors, self, object); | |
} | |
return NO; | |
} | |
@end | |
#pragma mark - | |
/* | |
* Compare two objects using a C-style array of selectors. | |
*/ | |
static bool compareBySelectors(SEL selectors[], NSObject* object1, NSObject* object2) | |
{ | |
int selectorTypeSize = sizeof(SEL); | |
int selectorArraySize = sizeof(*selectors); | |
int selectorCount = selectorArraySize / selectorTypeSize; | |
for (int i=0; i<selectorCount; i++) | |
{ | |
#pragma clang diagnostic push | |
#pragma clang diagnostic ignored "-Warc-performSelector-leaks" | |
SEL selector = selectors[i]; | |
id value1 = [object1 performSelector:selector]; | |
id value2 = [object2 performSelector:selector]; | |
#pragma clang diagnostic pop | |
BOOL equal; | |
if ([value1 respondsToSelector:@selector(ys_isEqual:)] && [value2 respondsToSelector:@selector(ys_isEqual:)]) | |
{ | |
equal = ((value1 == value2) || [value1 ys_isEqual:value2]); | |
} | |
else | |
{ | |
equal = ((value1 == value2) || [value1 isEqual:value2]); | |
} | |
if (!equal) | |
{ | |
return NO; | |
} | |
} | |
return YES; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment