I get the following problem:
'NSInvalidArgumentException', reason: 'Unable to generate SQL for predicate (category == Basis) (problem on RHS)'
case "Basisfragen":
request.predicate = NSPredicate(format: "category = Basis")
break
Does anybody have an idea why it doesn't work?
Wasn't able to find a helpful answer for Swift or to translate any Objective-C answer to a working one in Swift...
Thx!
Patrick
use this that assume that category is a key or attribute with string type.
request.predicate = NSPredicate(format: "category ==%#","Basis")
Related
I would like to make predicate more flexible for changes. I have lot of such strings in the sources code:
let predicate = NSPredicate(format: "bodyPart.name = %#", name)
let filteredExercises = ExerciseEntity.mr_findAll(with: predicate)
but as you see if I will change BodyPart name to MuscleGroup for example someday it will cause issue like:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'keypath bodyPart.name not found in entity <NSSQLEntity ExerciseEntity id=5>'
I am searching for the solution with a key paths like
let predicate = NSPredicate(format: "%# = %#", \BodyPart.name, name)
Use %K to substitute the key path
let predicate = NSPredicate(format: "%K = %#", #keyPath(BodyPart.name), name)
My Entity Office have set of File entities. Each File has uuid property. I need to fetch offices where we have any file where UUID is not in set of Strings
I tried to use
let predicate = NSPredicate(format: "NOT (ALL files.uuid IN %#)", uuidSet)
let predicate = NSPredicate(format: "ANY files.uuid NOT IN %#", uuidSet)
let predicate = NSPredicate(format: "ANY NOT files.uuid IN %#", uuidSet)
I always get the error like
exception 'NSInvalidArgumentException', reason: 'Unsupported predicate NOT ALL files.uuid IN {"C0DF0E67-ED8A-4D3B-87A1-E4E7B86967AA"}'
Is there a way to write this Predicate properly? Because right now I only see the solution to load all offices and then iterate through every item.
I am facing a problem when I am fetching data from Core data database in my iPhone application.
I am using this code (Line 1) which is working fine, here I am fetching data from a date:
Note: Datatype of date is NSDate and selectedDate is also type of NSDate.
Line1:
[NSPredicate predicateWithFormat:#"date == %#",selectedDate];
This code (Line 2) is also working fine When I am fetching data of a system:
Line 2:
[NSPredicate predicateWithFormat:[NSString stringWithFormat:#"systemName == '%#'",selectedSystemString]];
But when I combined the above two predicates with this code (Line 3):
Line 3:
[NSPredicate predicateWithFormat:[NSString stringWithFormat:#"(date == %#) AND (systemName == '%#')",selectedDate,selectedSystemString]];
And application crashed on Line 3, and this error message is displayed in console:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unable to parse the format string "(date == 2015-01-15 18:30:00 +0000) AND (systemName == 'A')"'
*** First throw call stack:
(0x26494c1f 0x33c9cc8b 0x270feb55 0x270fc9bd 0x270fc93d 0x9b263 0x9c143 0xa8815 0x29a69f97 0x29b1bc0f 0x299cdc4d 0x29949aab 0x2645b3b5 0x26458a73 0x26458e7b 0x263a7211 0x263a7023 0x2d75a0a9 0x299b31d1 0xb1e7d 0x3421caaf)
libc++abi.dylib: terminating with uncaught exception of type NSException
The difference between your first two lines is why the third one fails. (The second one is also incorrect but works because you're using strings.)
It's like using query parameters if you're familiar with SQL.
In line 1 you say, here's what I'm looking for with this parameter (which happens to be a date).
In line 2 you say, here's what I'm looking for. There are no parameters. It should be like this:
[NSPredicate predicateWithFormat:#"systemName == %#",selectedSystemString];
Line 3 fails because you convert the date into a string (which uses the description method on NSDate); your predicate has no parameters. It should look more like this:
[NSPredicate predicateWithFormat:#"(date == %#) AND (systemName == %#)",selectedDate,selectedSystemString];
try using compound predicate,
NSPredicate *datePredicate = [NSPredicate predicateWithFormat:#"date == %#",selectedDate];
NSPredicate *stringPredicate = [NSPredicate predicateWithFormat:#"systemName == %#",selectedSystemString];
NSPredicate *predicate = [NSCompoundPredicate andPredicateWithSubpredicates: #[datePredicate, stringPredicate]];
compound predicate using andPredicateWithSubpredicates: method is similar to AND predicate, the solution using AND would be
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"date == %# AND systemName == %#",selectedDate, selectedSystemString];
I tried some thread here, but I can't find the solution for my problem.
I am trying to write a NSPredicate like this:
NSPredicate* predicate = [NSPredicate predicateWithFormat: [NSString stringWithFormat:#"%# IN %#", column, list]];
Where the value of the column variable is #"id" and the list contains four number. The id column is a NSNumber column.
I get the following error, and I don't know why:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unable to parse the format string "id IN (
8,
5,
15,
22
)
The problem is not the #"id" named column (like the type), I tried to changed that and it doesn't work.
How should I properly use the IN operator with core data?
Don't mix stringWithFormat and predicateWithFormat. And (as already commented above),
use %K for key paths:
NSPredicate* predicate = [NSPredicate predicateWithFormat:#"%K IN %#", column, list];
I tried to predicate my coredate based on the mood on the initial queries all the moods are set to 8.
I will be calling from 0 - 7 every sec to update the tableview.
but I got
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Can't use in/contains operator with collection 8 (not a collection)'
Should I use CONTAINS or other operator to predicate?
NSString* filter = #"%K CONTAINS[cd] %#";
NSPredicate *getMoodPred = [NSPredicate predicateWithFormat:filter, #"mood", mood];
NSLog(#"getmood %#",getMoodPred); //getmood mood CONTAINS[cd] "7"
NSArray *getMoodArray = [[VPPCoreData sharedInstance]allObjectsForEntity:#"Song" orderBy:Nil filteredBy:getMoodPred];
The probably means that the mood attribute is stored as Number, not as String. "CONTAINS" works only with strings. The following predicate should work:
NSNumber *mood = #(7);
NSString *filter = #"%K == %#";
NSPredicate *getMoodPred = [NSPredicate predicateWithFormat:filter, #"mood", mood];
using NSNumber on the rhs and == as comparator.