Originally created by @vovsyannikov on GitHub (Jun 20, 2024).
In my project there's a stored property with search info. What I would like to do is fetch all the objects that contains some searchable string. Right now the only way I see doing it is through unsafe NSPredicate where I can't even user #keyPath(Document.searchInfo) to alleviate some of the risks
Am I missing something or is it the only way of handling CONTAINS in CoreStore?
Originally created by @vovsyannikov on GitHub (Jun 20, 2024).
In my project there's a stored property with search info. What I would like to do is fetch all the objects that contains some searchable string. Right now the only way I see doing it is through unsafe `NSPredicate` where I can't even user `#keyPath(Document.searchInfo)` to alleviate some of the risks
```Swift
public func count(where predicate: Where<DBS.Document>, searchString: String? = nil) -> Int {
do {
var wholePredicate = predicate
if let searchString, searchString.isEmpty == false {
let searchPredicate = NSPredicate(format: "searchInfo CONTAINS %@", string)
wholePredicate = wholePredicate && Where(searchPredicate)
}
let count = try transaction.fetchCount(
From<DBS.Document>()
.where(wholePredicate)
)
return count
} catch {
print("Error fetching document count: \(error.localizedDescription)")
return 0
}
}
```
Am I missing something or is it the only way of handling `CONTAINS` in CoreStore?
Yes, the type-syntax utilities are currently missing several predicate features. You can always write the raw String format for now, but as it is I'm still observing how the SwiftData utilities are evolving to see which ones we can hook up with CoreStore.
As always, if you have proposed implementations feel free to submit a PR.
@JohnEstropia commented on GitHub (Jun 21, 2024):
Yes, the type-syntax utilities are currently missing several predicate features. You can always write the raw String format for now, but as it is I'm still observing how the SwiftData utilities are evolving to see which ones we can hook up with CoreStore.
As always, if you have proposed implementations feel free to submit a PR.
@JohnEstropia commented on GitHub (Jun 21, 2024):
Just a tip though, you can use `%K` so that you can still take advantage of `#keyPath`:
```swift
NSPredicate(format: "%K CONTAINS %@", #keyPath(Document.searchInfo), string)
```
Yes I am aware of such method. However that's the problem: I get en error that searchInfo is not @objc marked. It's a @Field.Stored property so it seems that stringly typed NSPredicate is the only way out
@vovsyannikov commented on GitHub (Jun 21, 2024):
> Just a tip though, you can use `%K` so that you can still take advantage of `#keyPath`:
>
> ```swift
> NSPredicate(format: "%K CONTAINS %@", #keyPath(Document.searchInfo), string)
> ```
Yes I am aware of such method. However that's the problem: I get en error that `searchInfo` is not `@objc` marked. It's a `@Field.Stored` property so it seems that stringly typed `NSPredicate` is the only way out
@JohnEstropia commented on GitHub (Jun 21, 2024):
Ah, I see. Sorry, I thought your model was using the `NSManagedObject` mode. For the `CoreStoreObject` subclasses, you can do this instead:
```swift
NSPredicate(format: "%K CONTAINS %@", String(keyPath: \Document.$searchInfo), string)
```
@vovsyannikov commented on GitHub (Jun 21, 2024):
> Ah, I see. Sorry, I thought your model was using the `NSManagedObject` mode. For the `CoreStoreObject` subclasses, you can do this instead:
>
> ```swift
> NSPredicate(format: "%K CONTAINS %@", String(keyPath: \Document.$searchInfo), string)
> ```
Oooh. That's neat. Thanks!
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.
Originally created by @vovsyannikov on GitHub (Jun 20, 2024).
In my project there's a stored property with search info. What I would like to do is fetch all the objects that contains some searchable string. Right now the only way I see doing it is through unsafe
NSPredicatewhere I can't even user#keyPath(Document.searchInfo)to alleviate some of the risksAm I missing something or is it the only way of handling
CONTAINSin CoreStore?@JohnEstropia commented on GitHub (Jun 21, 2024):
Yes, the type-syntax utilities are currently missing several predicate features. You can always write the raw String format for now, but as it is I'm still observing how the SwiftData utilities are evolving to see which ones we can hook up with CoreStore.
As always, if you have proposed implementations feel free to submit a PR.
@JohnEstropia commented on GitHub (Jun 21, 2024):
Just a tip though, you can use
%Kso that you can still take advantage of#keyPath:@vovsyannikov commented on GitHub (Jun 21, 2024):
Yes I am aware of such method. However that's the problem: I get en error that
searchInfois not@objcmarked. It's a@Field.Storedproperty so it seems that stringly typedNSPredicateis the only way out@JohnEstropia commented on GitHub (Jun 21, 2024):
Ah, I see. Sorry, I thought your model was using the
NSManagedObjectmode. For theCoreStoreObjectsubclasses, you can do this instead:@vovsyannikov commented on GitHub (Jun 21, 2024):
Oooh. That's neat. Thanks!