nspredicate

Core Data Predicate Filter By Today's Date

帅比萌擦擦* 提交于 2019-11-30 13:37:51
I had issues with this and I haven't found a proper answer on SO so I'll leave a small tutorial here. The goal is to filter fetched objects by today's date. Note: It's Swift 3 compatible. Lawrence413 You can't simply use to compare your date to today's date: let today = Date() let datePredicate = NSPredicate(format: "%K == %@", #keyPath(ModelType.date), today) It will show you nothing since it's unlikely that your date is the EXACT comparison date (it includes seconds & milliseconds too) The solution is this: // Get the current calendar with local time zone var calendar = Calendar.current

NSPredicate Returns No Results with Fetch Request, Works with Array Filtering

梦想与她 提交于 2019-11-30 13:28:59
My situation is simple: I have some records in my core data store. One of their attributes is a string called "localId". There's a point where I'd like to find the record with a particular localId value. The obvious way to do this is with an NSFetchRequest and an NSPredicate. However, when I set this up, the request returns zero records. If, however, I use the fetch request without the predicate, returning all records, and just loop over them looking for the target localId value, I do find the record I'm looking for. In other words, the record is there, but the fetch request can't find it. My

How to sort NSPredicate

我的未来我决定 提交于 2019-11-30 13:01:37
问题 I am trying to sort my array while using NSPredicate.. I have read other places that possibly using NSSortDescriptor could be an option. Having some trouble figuring this out. I am attempting to sort my array by companyName. Any advice appreciated, thanks Greg - (void)filterSummaries:(NSMutableArray *)all byNameThenBooth:(NSString*) text results:(NSMutableArray *)results { [results removeAllObjects]; if ((nil != text) && (0 < [text length])) { if ((all != nil) && (0 < [all count])) {

NSPredicate something equivalent of SQL's GROUP BY

不想你离开。 提交于 2019-11-30 12:52:34
To simplify: There are 3 columns in a table named cards. id packTitle term id is a column - integers from 0.....100 packTitle - string describing packs, lets say there are 3 kinds of pack PACK1, PACK2, PACK3 term - different unsorted names of 101 items. by SQL statement select packTitle from cards group by packTitle; I can get list of 3 items. Could you suggest NSPredicate equivalent of SQL statement GROUP BY. I need to get an array to populate in UITableView. Barry Wark CoreData is an object graph management framework, not a SQL data store. Thus, you should—as quickly as possible—get yourself

How to use binary flags in Core Data?

人盡茶涼 提交于 2019-11-30 11:43:11
问题 I have an int32 attribute in a Core Data database. I use this int as an enum bit field. Is it possible to create a NSPredicate to query items based on the binary value of this int ? Something like @"bitFieldAttribute & 0x0001" ? I'm also wondering if this is possible with a binary typed attribute ? 回答1: NSPredicate can handle it, but I'm not sure if CoreData will accept it as a valid predicate for execution on a data store. It might have trouble converting the bitwise operator into a SQL

Sorting NSMutableArray using SortDescriptor AND Predicate possible?

こ雲淡風輕ζ 提交于 2019-11-30 10:01:57
I have an array of type "Restaurant" which has an NSSet of "Rating." Rating has an ID and a value. I want to sort the array of Restaurant's by rating with an ID of 01, from high to low. Something like the following, but how do I use the predicate and sort descriptor together on originalArray? NSPredicate *predicate = [NSPredicate predicateWithFormat:@"Rating.rating_id=%d", ratingID]; NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:@"currentValue" ascending:YES]; NSArray *sorted = [[originalArray allObjects] sortedArrayUsingDescriptors:[NSArray arrayWithObject:descriptor]

NSPredicate Aggregate Operations with NONE

☆樱花仙子☆ 提交于 2019-11-30 08:43:53
问题 How do I create a predicate that can fetch: all questions does not contain answer.correct = "1" . The following predicate doesn't work if the returned array contain "0" and "1": [NSPredicate predicateWithFormat:@"NONE answers.correct IN %@", [NSArray arrayWithObject:@"1"]]; Also tried with NOT (ANY ...) : same result Is this a Bug? 回答1: Short answer: To get all objects that do not have any "answer" with "correct == 1", use the following SUBQUERY : [NSPredicate predicateWithFormat:@"SUBQUERY

Core Data unsupported predicate with ALL and IN

两盒软妹~` 提交于 2019-11-30 07:53:00
I have a request like this : NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY attributes.attribute.attributeId IN %@", attributeIds]; That will return a list of objects, that have one ore more of the attributes I set. I want to get a list of objects that have all the attributes I pass, so I tried : NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ALL attributes.attribute.attributeId IN %@", attributeIds]; But I got an exception : 'NSInvalidArgumentException', reason: 'Unsupported predicate (null)' Anyway, I'm not even sure that this is the right request. Say that I

NSPredicate not executed

好久不见. 提交于 2019-11-30 07:18:25
This is quite funny. In my application I create thousands of entry in the database (in another thread, I'm using MagicalRecord). Everything seems working fine (from a background/foreground/context point of view). When, in the main thread, I try to fetch the "just inserted" data, I discovered the following behaviour: - (NSArray *) familiesInCompany:(Company *) company { NSPredicate *predicate1 = [NSPredicate predicateWithFormat:@"company == %@", company]; NSPredicate *predicate2 = [NSPredicate predicateWithFormat:@"company.name == %@", company.name]; NSArray *first = [Family MR_findAllSortedBy:

Evaluating an NSPredicate on a NSArray (without filtering)

爷,独闯天下 提交于 2019-11-30 07:16:43
Is it possible to evaluate a NSPredicate on a NSArray, without having the NSPredicate starting to filter out objects in the array? For instance, say I have the following predicate that just checks the number of objects in an array: NSPredicate *pred = [NSPredicate predicateWithFormat:@"count == 3"]; NSArray *list = [NSArray arrayWithObjects:@"uno", @"dos", @"volver", nil]; BOOL match = [pred evaluateWithObject:list]; This will crash, as pred will try to retrieve the "count" key from the first object in the array instead of the array itself. EmptyStack Use the SIZE operator of NSPredicate which