Filter Objects by transient (computed) properties
Terminating app due to uncaught exception 'Invalid property name', reason: 'Property 'isImage' not found in object of type 'Attachment''
class Post: Object {
let attachedFiles = List<Attachment>()
var imageAttachments:Results<Attachment>?{
return self.attachedFiles.filter(NSPredicate(format:"isImage = true"))
}
}
class Attachment: Object {
dynamic var contentType: String?
var isImage:Bool {
if let contentType = contentType {
return contentType.hasPrefix("image")
}
return false
}
}
let post //a single Post object
let images = post.imageAttachments //crashes
Realm version: 2.1.1
Xcode version: 8.
iOS/OSX version: 10
Dependency manager + version: Cocoapods 1.1.1
What you refer to as "transient (computed) properties", Realm refers to as "ignored properties". These are properties that are for the most part ignored by Realm, so they won't be stored in the db file, can be mutated outside write transactions, etc.
However, this also means that they don't benefit from many of the capabilities of non-ignored properties, such as queries.
This feature would be nice to have, but isn't trivial to implement and is blocked on some core functionality.
@jpsim Thanks for the clarification. Can I recommend adding some notes in the documentation? It's not clear there that ignored properties are excluded from queries. The docs make it sound like they are just not persisted.
https://realm.io/docs/swift/latest/#ignored-properties
Override Object.ignoredProperties() to prevent Realm from persisting model properties. Realm won鈥檛 interfere with the regular operation of these properties
Thanks for the suggestion!
I'm in the process of adding this paragraph to that section of the docs to clarify (pending review and feedback):
Because these properties are almost entirely ignored by Realm, they can't be
used in queries, they won't retain their value when retrieving different
instances the same object, won't trigger notifications when changed, etc.
They can still be KVO-observed, however.
Most helpful comment
Thanks for the suggestion!
I'm in the process of adding this paragraph to that section of the docs to clarify (pending review and feedback):