Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport Foundation Predicates #21

Open
helje5 opened this issue Oct 4, 2023 · 2 comments
Open

Backport Foundation Predicates #21

helje5 opened this issue Oct 4, 2023 · 2 comments
Labels
enhancement New feature or request

Comments

@helje5
Copy link
Contributor

helje5 commented Oct 4, 2023

The Foundation Predicate is available in the open source Foundation. I think it requires iOS 17+ because it uses variadic generics. But that's not actually necessary for the SwiftData APIs which only pass in a single model type.

This should probably go into an own package.

@helje5 helje5 added the enhancement New feature or request label Oct 4, 2023
@HealsCodes
Copy link

Has there ever been any progress towards this?

I've been playing around with Predicates + ManagedModels but always run into a dead-end seemingly because Model properties aren't exposed to the underlying Objective-C runtime so code like this doesn't work:

@Model
final class MyModel: NSManagedObject {
  var enabled: Bool

  convenience init() {
    self.enabled = false
  }
}

// the following works if MyModel is created using the traditional NSManagedObject / @NSManaged means

let p = #Predicate<MyModel> { $0.enabled == true } 
let predicate = NSPredicate(p) // returns nil, because apparently MyModel.enabled isn't bridged

Likewise a simple NSExpression(forKeyPath: \MyModel.enabled) fails with "Foundation/NSObject.swift:132: Fatal error: Could not extract a String from KeyPath \MyModel.enabled"

The same issue breaks interoperability with packages like PredicateKit which relies on being able to convert AnyKeyPath to String using ObjC bridging internals.

@helje5
Copy link
Contributor Author

helje5 commented Oct 29, 2024

I haven't seen a Predicate back port yet, but that seems unrelated to your issue (and this becomes less necessary as time goes by). I've created #36 for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants