Comments (14)
@tybro0103 sounds good. There's also a (maybe) simpler syntax for the end users, what do you think about this one?
+ (NSArray *)where:(id)condition orderBy:(NSString *)key ascending:(BOOL)ascending;
from objectiverecord.
Usage:
1st example
[Person where:@{ @"name": @"Steve" } order: @{ @"surname": @"desc" }];
2nd example
[Person where:@{ @"name": @"Steve" } orderBy:@"surname" ascending:NO];
Maybe simpler wasn't the right term, I've meant to say 'more obvious'.
from objectiverecord.
Looks alright, only problem is ordering by more than one attribute.
In the AR guides it just shows using a string:
http://guides.rubyonrails.org/active_record_querying.html#ordering
The docs have a hash example:
http://api.rubyonrails.org/classes/ActiveRecord/QueryMethods.html#method-i-order
from objectiverecord.
@tybro0103 Looks like it's almost possible to specify several attribues with the 1st example provided by @mneorr ; however, it's not possible to define priorities between attributes. Could be a right fix to provide a NSArray with several NSDictionnary.
[Person where:@{ @"name": @"Steve" } order: @[ @{@"surname" : @"desc", @"firstname" : @"asc"} ]];
from objectiverecord.
@tybro0103 Did you mean the following?
[ Person where: @{ @"name": @"Steve" } order: @[ @{@"surname" : @"desc" }, @{@"firstname" : @"asc"} ] ];
Now, the order clause is properly applied from left to right and the key ordering is maintained within it. However, if you were passing a single NSDictionary, then key ordering wouldn't matter. For example, the following should be possible:
[ Person where: @{ @"name": @"Steve" } order: @[ @{@"surname" : @"desc" } ] ];
will generate the same results as
[ Person where: @{ @"name": @"Steve" } order: @{@"surname" : @"desc" } ];
In short, key ordering starts to matter when you're using 2 or more keys for the order clause.
from objectiverecord.
@conradwt yes, looks good ;)
from objectiverecord.
@conradwt Actually that's what I was thinking about, but I wrote something different. That's the idea to pass an array of dictionaries, thx!
from objectiverecord.
extend fetchWithPredicate:inContext: (sorry for inline code)
// MARK: example: @"name Asc , title desc, amount"
+ (NSArray *)sortDescriptorsFromString:(NSString*)string {
// if (![sorting isKindOfClass:[string class]] || string.length == 0) return nil;
NSArray *sorting = [[string split:@","] map:^(NSString *substring) {
NSArray *tokens = [[substring strip] split:@" "];
if (tokens.count > 2) return nil;
NSString *key= [tokens.first strip];
BOOL ascending = YES;
if (tokens.count == 2)
ascending = ![[tokens.last lowercaseString] isEqualToString:@"desc"];
return [NSSortDescriptor sortDescriptorWithKey:key ascending:ascending];
}
return sorting;
}
// MARK: example: @{" name" : @"desc ", @"title": @"ASC"}
+ (NSArray *)sortDescriptorsFromDictionary:(NSDictionary *)dict {
// if (![dict isKindOfClass:[NSDictionary class]]) return nil;
NSArray *sorting = [dict map:^(id key, id value) {
// if (![key isKindOfClass:[NSString class]]
// || ![value isKindOfClass:[NSString class]]) return nil;
NSString *keyString = [key strip];
// if (!keyString.length) return nil;
NSString *sortString = [value lowercaseString];
BOOL ascending = ![sortString isEqualToString:@"desc"];
return [NSSortDescriptor sortDescriptorWithKey:key ascending:ascending];
}
return sorting;
}
// MARK: example: @[@" amount", @{" name" : @"desc ", @"title": @"ASC"}]
+ (NSArray *)sortDecriptorsFromArray: (NSArray *)array {
// if (![array isKindOfClass:[NSArray class]]) return nil;
NSArray *sortDescriptors = [array map:^(id object) {
if ([object isKindOfClass:[NSString class]]) {
return [self sortDescriptorsFromString:object];
} else if ([object isKindOfClass:[NSDictionary class]]) {
return [self sortDescriptorsFromDictionary:object];
}
}
return [sortDescriptors flatten];
}
+ (NSArray *)fetchWithPredicate:(NSPredicate *)predicate
inContext:(NSManagedObjectContext *)context {
[return self fetchWithPredicate:predicate
orderBy:nil
inContext:context];
}
+ (NSArray *)fetchWithPredicate:(NSPredicate *)predicate
orderBy:(id)sorting
inContext:(NSManagedObjectContext *)context {
NSFetchRequest *request = [self createFetchRequestInContext:context];
[request setPredicate:predicate];
if ([sorting isKindOfClass:[NSString class]]) {
[request setSortDescriptors:[self sortDecriptorsFromString:sorting];
else if ([sorting isKindOfClass:[NSDictionary class]]) {
[request setSortDescriptors:[self sortDecriptorsFromDictionary:sorting];
else if ([sorting isKindOfClass:[NSArray class]])
[request setSortDescriptors:[self sortDecriptorsFromArray:sorting];
NSArray *fetchedObjects = [context executeFetchRequest:request error:nil];
return fetchedObjects.count > 0 ? fetchedObjects : nil;
}
from objectiverecord.
NOTE: every dictionary should have only one object, because search result depends on sorting keys order (NSDictionary isn't ordered collection)
from objectiverecord.
@viking2009 hope you don't mind editing your comment, I've just applied some code formatting.
Whenever you embed inline code, you may want to surround it like this:
// code here
from objectiverecord.
OK, thanks
from objectiverecord.
and one more thing
+ (NSArray *)sortDecriptorsFromArray: (NSArray *)array {
// if (![array isKindOfClass:[NSArray class]]) return nil;
NSArray *sortDescriptors = [array map:^(id object) {
//
else if ([object isKindOfClass:[NSSortDescriptor class]]) {
return object;
} else {
return nil;
}
//
+ (NSArray *)fetchWithPredicate:(NSPredicate *)predicate
orderBy:(id)sorting
inContext:(NSManagedObjectContext *)context {
//
else if ([sorting isKindOfClass:[NSSortDescriptor class]])
[request setSortDescriptors:@[sorting]];
//
from objectiverecord.
// MARK: example: @{" name" : @"desc ", @"title": @"ASC"}
+ (NSArray *)sortDescriptorsFromDictionary:(NSDictionary *)dict {
// if (![dict isKindOfClass:[NSDictionary class]]) return nil;
if ([dict count] != 1) return nil;
from objectiverecord.
this is in master
from objectiverecord.
Related Issues (20)
- fetching max min and other sql functions HOT 1
- Integrating ObjectiveRecord with iCloud HOT 2
- Swift Example HOT 3
- NSFetchedResultsController HOT 1
- Save completion handler? HOT 1
- Issue with Swift and XCode 6.1 HOT 3
- Sill alive? HOT 1
- Is there a Swift version coming out? HOT 5
- Adding NSPersistentStoreUbiquitousContentNameKey to options dictionary
- Readme - Block based NSPredicate HOT 1
- Testing failed: Use of undeclared identifier '_description'; did you mean 'aDescription'? ** TEST FAILED **
- .create() call broken in Swift as of Swift 1.2 (xcode 6.3-beta) HOT 6
- 64 bit integers on non 64 bit architectures HOT 2
- Carthage error ?
- How should I delete a cascade entity? HOT 1
- Save Only the Object HOT 3
- Sort Methods missing from Pod Version HOT 1
- Possible bug introduced in 1.5 HOT 6
- Find an entity which there is only one entity HOT 4
- Nested objects won't get mapped
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from objectiverecord.