Another workaround is:
Enter the property taskCount
in TaskList
and always synchronize taskCount
and tasks.count
.
class TaskList: Object {
dynamic var name = ""
dynamic var createdAt = NSDate()
let tasks = List<Task>()
dynamic var taskCount = 0
}
Then you can use
realm.objects(TaskList).sorted("taskCount")
Realm , .
taskCount
tasks.count
, :
( tasks.append()
, addTask()
.)
class TaskList: Object {
dynamic var name = ""
dynamic var createdAt = NSDate()
private let tasks = List<Task>()
dynamic var taskCount = 0
func addTask(task: Task) {
func add() {
tasks.append(task)
taskCount = tasks.count
}
if let realm = realm where !realm.inWriteTransaction {
try! realm.write{
add()
}
} else {
add()
}
}
}