UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Day 61 challenge - 'NSInvalidArgumentException', reason: 'unimplemented SQL generation for predicate...

Forums > 100 Days of SwiftUI

Hi all,

I'm implementing a "DetailedUserView" for the Dore Data challenge part (day 61)

This view contains a generic FilteredList (form Core Data technique project, Project 12 part 2) for Friends of the current User. To this list I will pass a predicate to retrieve from database the complete information for all Friends, so in the init for my DetailedUserView I prepare everything I need to pass dow to this generic list view:

struct DetailedUserView: View {

    @State private var activeFriendsOnly = false

    private let user: CDUser
    private let friendsID: [UUID]
    private let friendsPredicate: NSPredicate

    init(activeFriendsOnly: Bool = false, user: CDUser) {
        self.user = user
        self.friendsID = user.wrFriends.map{ $0.wpId }
        self.friendsPredicate = NSPredicate(format: "wpID IN %@", argumentArray: friendsID)
        self.activeFriendsOnly = activeFriendsOnly

    }

    var body: some View {
    ...

(wr and wp prefixes indicate that these are wrapping-computed-properties/relationship to not deal with the optionals from Core Data, as in Creating NSManagedObject subclasses, added as extensions to the generated cclasses)

The probem is that the app crashes when entering the DetailUserView with the error:

unimplemented SQL generation for predicate : (wpID IN 3E6FA1D2-527C-41E9-9DA0-2D89EB0B8D6A)'

The example for this type of predicate is at https://nspredicate.xyz/#predicate-format-and-arguments

Examples
Is included in an Array of values
let wantedItemIDs = [1, 2, 3, 5, 8, 13, 21]

// Retrieve record with item_id which is inside the wantedItemIDs array
let inclusivePredicate = NSPredicate(format: "item_id IN %@", wantedItemIDs)

Curiously:

if I use the predicate from the example above (without the label argumentArray:) I got the error:

NSInvalidArgumentException', reason: 'unimplemented SQL generation for predicate : (wpID IN {3E6FA1D2-527C-41E9-9DA0-2D89EB0B8D6A, 29E0F9EE-71F2-4043-AD36-9D2D6789B2C8, 8BE513E0-B46D-40CC-B617-A295A26525DE})'

which seems to consider the whole array...

...but if I use the Predicate with the label argumentArray: I get the error:

'NSInvalidArgumentException', reason: 'unimplemented SQL generation for predicate : (wpID IN 3E6FA1D2-527C-41E9-9DA0-2D89EB0B8D6A)'

which seems to consider ony the first element of the array :-)

I tried also with strings (converting the UUID or using user name) but I always get this error.

Did anyone met and solved this error?

Is there another simple way to filter Core Data objects to fetch only those with an attribute value contained in an array/set/collection ?

I can give acess to my complete code on gitHub if needed (not public by defaut because I'm, code-shy:-)), let me know

3      

Try casting the UUID to a CVarArg before using it in an NSPredicate. You apparently can't just stick a UUID into a predicate like that.

3      

Hi @roosterboy,

thanks for your sugestion!

I'm not sure I got it right, did you mean somethig like ?

...
self.friendsPredicate = NSPredicate(format: "name IN %@", argumentArray: friendsID as? CVarArg)
...

?

This gives an error and the compiler suggests to fix by casting to [Any], so I ended up like this:

...
self.friendsPredicate = NSPredicate(format: "name IN %@", argumentArray: friendsID as? CVarArg as! [Any])
...

Unfortunately, it doesn't solved the problem, same crash.

Somehow I expected this since I already tried using the friend name (String) instead of its id (UUID):

private let friendsID: [String]
...
self.friendsID = user.wrFriends.map{ $0.wpName }
self.friendsPredicate = NSPredicate(format: "wpName IN %@", argumentArray: friendsID)
...

with the same results (also trierd to apply your casting here, same result).

However this was useful because by doing this I noticed an error in my code:

I was using wpID (or wpName) in the predicate, but these are my computed vars... I should have used id or name instead , the original properties of the entity.

So I corrected like this:

struct DetailedUserView: View {

    @State private var activeFriendsOnly = false
    @State private var numberOfFriends = 0

    private let user: CDUser
    private let friendsID: [String]
    private let friendsPredicate: NSPredicate

    init(activeFriendsOnly: Bool = false, user: CDUser) {
        self.user = user
        self.friendsID = user.wrFriends.map{ $0.wpName }
        self.friendsPredicate = NSPredicate(format: "name IN %@", argumentArray: friendsID)
        self.activeFriendsOnly = activeFriendsOnly

    }

    var body: some View {
    ...

And i got a different error:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString countByEnumeratingWithState:objects:count:]: unrecognized selector sent to instance 0x6000038f17e0' terminating with uncaught exception of type NSException CoreSimulator 732.18.6 - Device: iPhone 12 Pro (B6BCCAA3-F364-4237-A955-FBA9E6333211) - Runtime: iOS 14.4 (18D46) - DeviceType: iPhone 12 Pro

the same happen if i apply your suggestion again (cast to CVarArg and Any).

I'm stuck, this exception is even more obscure than the previous :-)

3      

Solved,

apparently I didn't try everything in the right combnation:

using properties from generated classes (not the wrapping ones) in the predicate AND removing the label

argumentArray:

from the predicate initializer works!

Still confused however, I'd like to know the root cause since that argument label is also in Apple official docuentation

3      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.