I have a query using
NSPredicate that takes regular expression and matches to paths:
let predicate = NSPredicate(format: "path MATCHES '/test/([^/]*[^/])$'")
which matches everything inside folder test but excludes all subfolders of folders in test, like this:
- /test/abc -> yes
- /test/abc/def -> no
The problem is, that I can't use the
MATCH keyword of
NSPredicate in Realm.io. How can I reproduce this query so it works in Realm?
Adding the exception for completeness:
*** Terminating app due to uncaught exception 'Invalid operator type', reason: 'Operator type 6 not supported for string type'
Best How To :
So for now I have solved the problem like this (modifying the model, which I wanted to avoid):
- I store only the path to the parent, not the full one along with the name. The name gets stored in a different field (to get full path I stitch the two strings together)
- Instead of matching the regular expression I use
NSPredicate(format: "path ==[c] %@", "/test/") to get the contents of the folder test.
Why am I not totally happy with this solution:
I wanted to avoid separating the field fullPath into two fields because when file gets renamed + moved, it's only one operation - rewrite the fullPath in the database. It may seem like a minor problem, but having only one place of responsibility gave it a bit more robustness, because files are moved and renamed all the time in the environment of the app.