Sorting Realm results collection on computed property?
As you suspect, you cannot persist a computed property and Realm
's sorted(byKeyPath:,ascending:)
method only works with persisted properties. There is no way to sort a Results
instance based on a computed property while keeping the auto-updating Results
.
However, there is a workaround if you don't need the auto-updating nature of Results
. You can use Swift's sorted
method, which takes a closure as its sorting parameter, which will result in a return value of type [Conversation]
:
let conversations = realm.objects(Conversation.self).sorted(by: {$0.lastUpdated < $1.lastUpdated})
Does Realm support set max record counts
No, that isn't supported directly, although it should be fairly simple to implement yourself.
On Android it would look something like this:
realm.beginTransaction();
// Delete all records more than 90 days old
realm.where(Record.class)
.lessThan("created", calculateCutoff())
.findAll()
.deleteAllFromRealm();
// Create new record
Record r = realm.createObject(Record.class);
r.setCreated(new Date())
...
realm.commitTransaction();
Update Additional Field on Realm Object with ObservedRealmObject in SwiftUI
There are a lot of ways this can be accomplished; from simply updating the updated property when the 'save' button is pressed to using KVO to observe changes to the objects name property to adding a Swift 'front end' to the objects objc properties that can be handled through Computed Properties.
Let's use the third option using Swift Computed Properties and modify the Item in the question.
class Item: Object, ObjectKeyIdentifiable{
@objc dynamic var _id = UUID().uuidString
@objc private dynamic var _name = ""
@objc dynamic var updated = Date()
var name: String {
get {
return _name
}
set {
_name = newValue
updated = Date()
}
}
convenience init(name: String) {
self.init()
self.name = name
}
override static func primaryKey() -> String? {
return "_id"
}
}
A few things to note:
The realm name
property was made private with a name change to _name
. I did that so you don't accidentally set it and go around the Swift implementation.
We then added a new Swift name
property that acts as the 'front end' property to a Realm property backed by _name
.
When an item is created using the convenience function let i = Item(name: "Item 0")
or set within a write block someItem.name = "Updated name"
the name
computed property sets the Realm property and also populates the updated
property
set {
_name = newValue
updated = Date()
}
This should work seamlessly with @ObservedRealmObject
Realm database callbacks
What you are looking for are computed properties. However, Realm currently doesn't support storing computed properties, but you don't really need to persist fullName
in Realm, since as long as givenName
and familyName
are persisted, you can always compute fullName
.
class Contact: Object {
@objc dynamic var uid: String? = nil
@objc dynamic var givenName: String? = nil
@objc dynamic var familyName: String? = nil
var fullName: String? {
if let givenName = givenName, let familyName = familyName {
return "\(givenName) \(familyName)"
}
return nil
}
}
Swift Saving Structured Data to realm database
You need to persist the container too.
@Persisted var coordinates = List<TripRoute>()
The TripRoute
objects are probably stored in the db somewhere but there is nothing tying them to your TripModel
object.
Related Topics
Swiftui Previews - Unexpected Data
Swift Getnameinfo Unreliable Results for Ipv6
Ambiguous Use of Recover Error While Using Promisekit
How to Create Viewcontrollers Without Storyboard and Set One as Delegate of The Other One
Swift Equivalent of Unity3D Coroutines
Swiftui: Unable to Animate Images
How Does Swift Disambiguate Type Arguments in Expression Contexts
iOS-Charts Error: Thread1: Exc_Bad_Access (Code=2, Address=0X2A0C220)
How to Create Nested Dictionary Elements in Swift
How to Allow a Generic Collection to Perform Under The Hood Conversion in Swift
Getting Reference to a Dictionary Value
Weird Toolbar with Nested Conditionals Behavior
Compiling for iOS 10.3, But Module 'swiftuicharts' Has a Minimum Deployment Target of iOS 13.0
Swift "Print" Doesn't Appear in Stdout But 3Rd Party C Library Logs Do When Running in Docker on Ecs
Argument of '#Selector' Does Not Refer to an '@Objc' Method (Swift 3)
Dynamictype of Optional Chaining Not The Same as Assignment