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

Can I filter a PersistableEnum with predicate? #8423

Closed
cuongtv51 opened this issue Nov 25, 2023 · 4 comments
Closed

Can I filter a PersistableEnum with predicate? #8423

cuongtv51 opened this issue Nov 25, 2023 · 4 comments

Comments

@cuongtv51
Copy link

cuongtv51 commented Nov 25, 2023

How frequently does the bug occur?

Always

Description

Can I filter a PersistableEnum with predicate?
I search document, but not found solution

Stacktrace & log output

No response

Can you reproduce the bug?

Always

Reproduction Steps

No response

Version

all

What Atlas Services are you using?

Local Database only

Are you using encryption?

No

Platform OS and version(s)

iOS

Build environment

Xcode version: ...
Dependency manager and version: ...

@Jaycyn
Copy link

Jaycyn commented Nov 26, 2023

Can you clarify the question? Including some code that demonstrates what's being attempted and your models would help.

If you're asking about filtering on a Realm Object property that's set to a PersistableEnum value, the answer is yes. But if not, what's the use case for filtering an enum since it's essentially static data (a fixed set of related values)?

@cuongtv51
Copy link
Author

cuongtv51 commented Nov 26, 2023

Hi, great thanks your feedback?
we have models:

enum MSPlaylistType: String, PersistableEnum {
case systemMedia
case systemLastPlay
case systemFavorite
case systemHistory
case userCreated
case artist
case genre
case album
}

class MSPlaylist: Object, Identifiable {
@persisted(primaryKey: true) public var playlistId : String!
@persisted public var title : String?
@persisted(indexed: true) var playlistType: MSPlaylistType = .userCreated
}

How can I filter MSPlaylist with playlistType is album? I don't know write that filter with NSPredicate? I read document but not found.
Example:
let predicate = NSPredicate(format: "...")
Realm.objects(MSPlaylist.self).filter(predicate)
I mean how can I write NSPredicate to filter MSPlaylist with playlistType is album?

@Jaycyn
Copy link

Jaycyn commented Nov 26, 2023

You're going to love this!

Here are your original enum and object, noting I changed how the primaryKey is instantiated - nothing to do with the solution.

enum MSPlaylistType: String, PersistableEnum {
    case systemMedia
    case systemLastPlay
    case systemFavorite
    case systemHistory
    case userCreated
    case artist
    case genre
    case album
}

class MSPlaylist: Object, Identifiable {
    @Persisted(primaryKey: true) var _id: ObjectId
    @Persisted public var title : String?
    @Persisted(indexed: true) var playlistType: MSPlaylistType = .userCreated
}

And then some code to write an artist and genre and then query for them

let p0 = MSPlaylist()
p0.playlistType = .artist

let p1 = MSPlaylist()
p1.playlistType = .genre

try! realm.write {
    realm.add(p0)
    realm.add(p1)
}

and then query

let artistResults = realm.objects(MSPlaylist.self).where { $0.playlistType == .artist}
print(artistResults)

let genreResults = realm.objects(MSPlaylist.self).where { $0.playlistType == .genre}
print(genreResults)

There you go!

Try to implement some of the new styles of doing queries that are more type safe than Predicates. See here .where

Going forward try to ask general programming questions in the forums or on StackOverflow - leave git for more bug-level reporting issues.

@cuongtv51
Copy link
Author

Great thank your support.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 17, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants