sentry-cocoa icon indicating copy to clipboard operation
sentry-cocoa copied to clipboard

SwiftData: NSCompoundPredicate predicateOperatorType: unrecognized selector sent to instance

Open sowenjub opened this issue 2 years ago • 5 comments

Platform

iOS

Environment

Develop

Installed

Swift Package Manager

Version

8.18

Did it work on previous versions?

no

Steps to Reproduce

Use a SwiftData predicate that generates a ternary operator, in my case the code looks something like this

       let kinds: [AwardKind] = [.firstTime, .nightOwl]
       var descriptor = FetchDescriptor<Award>()
        let rawKinds = kinds.map(\.rawValue)
        
        descriptor.predicate = #Predicate<Award> { award in
            if let rawKind = award.rawKind {
                award.claimDate == nil && rawKinds.contains(rawKind)
            } else {
                award.claimDate == nil
            }
        }

Expected Result

Not crash

Actual Result

Crash in - (NSString *)comparisonPredicateDescription:(NSComparisonPredicate *)predicate because - (NSString *)predicateOperatorTypeDescription:(NSPredicateOperatorType)operator doesn't support TERNARY operator

Are you willing to submit a PR?

No response

sowenjub avatar Jan 10 '24 11:01 sowenjub

Hey @sowenjub - I don't see a reason to believe this is caused by Sentry, can you explain? Did you try to reproduce without Sentry? To me it looks like the predicate and/or how it's used to query might be incorrect.

kahest avatar Jan 10 '24 13:01 kahest

The query worked fine before I added Sentry, and still does if I don't init Sentry or if I disable Core Data tracing in the Sentry options.

The interruption points to Sentry code, the predicate generated by SwiftData includes a TERNARY operator which is not part of the supported operators in the function that crashes.

sowenjub avatar Jan 10 '24 13:01 sowenjub

Ah yes you're right - we'll fix this, thanks for bringing it to our attention

kahest avatar Jan 10 '24 14:01 kahest

Internal data points: https://sentry.sentry.io/issues/?project=4505469596663808&query=comparisonPredicateDescription&referrer=issue-list&statsPeriod=90d

kahest avatar Feb 07 '24 13:02 kahest

I also have this issue using SwiftData and compound Predicates:

https://letsrodeo.sentry.io/issues/5092468581/?referrer=alert_email&alert_type=email&alert_timestamp=1711126918556&alert_rule_id=15065067&notification_uuid=663b00cb-dd71-4ded-84cd-86543d085aec&environment=production

It works when sentry is removed

saulrodeo avatar Mar 22 '24 17:03 saulrodeo