everyone.
I've recently been using MongoDB Realm for Swift UI and it has been very good so far, but now I'm facing a problem that I have spent a lot of time trying to fix but to no avail.
To begin, I'm creating a simple To Do app to get used to the database, and I have created two entities, the Task, and the TaskGroup.
Naturally, there is a relationship between these two entities and that is a TaskGroup object contains many Task objects using List<Task>, and I would backlink the object using LinkingObjects<TaskGroup>.
Now, the issue is when I add a task group, there is no issues and the new task group is visible on the screen, but all hell breaks loose when I begin to add a task to the task group's tasks property, which is List<Task>, as for some reason, Realm throws an error saying that the task I'm appending is outside the realm transaction, even though it is, and I'm following the Realm tutorial.
I have a created a class to manage realms that I create, which includes a custom configuration (the rest of the code is singleton method and declaration):
class RealmManager
{
/* ... */
public static func Initialize() -> Void
{
/* Path to the realm. */
let path: String = ".../Markdown.realm"
/* The schema version must be updated every time we modify the schema. */
var config = Realm.Configuration /* -> */ (
schemaVersion: 6
)
config.fileURL = URL(fileURLWithPath: path)
Realm.Configuration.defaultConfiguration = config
}
/* ... */
}
Here are the class for the Task and TaskGroup entities:
final class Task: Object, ObjectKeyIdentifiable
{
// Attributes
@Persisted(primaryKey: true)
public var id: ObjectId
@Persisted
var title: String
@Persisted
var note: String
@Persisted
var date: Date
@Persisted
var complete: Bool
@Persisted
var priority: Int
@Persisted(originProperty: "tasks")
public var parent_group: LinkingObjects<TaskItemGroup>
/* ... */
}
final class TaskGroup: Object, ObjectKeyIdentifiable
{
@Persisted(primaryKey: true)
public var id: ObjectId
@Persisted
public var title: String
@Persisted
public var color: String
@Persisted
public var icon: String
@Persisted
public var tasks: RealmSwift.List<TaskItem>
/* ... */
}
And finally, here is the class where the issue is (I believe):
struct TaskAddSheet: View
{
/* ... */
@ObservedRealmObject
private var m_parent_task_group: TaskItemGroup
init(_ obj: TaskGroup)
{
m_parent_task_group = obj
}
private func AddTask() -> Void
{
do
{
let realm: Realm = try Realm()
let entry: TaskItem = TaskItem()
try realm.write {
entry.title = m_title
entry.note = m_note
entry.date = m_date
entry.priority = m_priority
m_parent_task_group.tasks.append(entry)
realm.add(entry)
}
}
catch
{
print("[Insert Error] - \(error.localizedDescription)")
}
/* Dismiss the current view. */
m_dismiss_action()
}
/* The rest is the view body. */
}
struct TaskGroupAddSheet: View
{
private func AddTaskGroup() -> Void
{
do
{
let realm: Realm = try Realm()
let entry: TaskItemGroup = TaskItemGroup()
try realm.write {
entry.title = m_title
entry.color = m_color
entry.icon = m_icon
entry.tasks = RealmSwift.List<TaskItem>()
realm.add(entry)
}
}
catch
{
print("[Error] - \(error)")
}
/* Dismiss the current view. */
m_dismiss_action()
}
/* The rest is the view body. */
}
Please let me know if you need more code samples so that I can add them if you need more information.
Thank you.
EDIT: I have trimmed the code and took out any empty initializers, line breaks, etc., to make reading the code clearer.
EDIT 2: I have renamed my entities Task to TaskItem, and TaskGroup to TaskItemGroup.
SOLVED: I needed to write $m_parent_task_group.append(entry) instead, which solved the problem. Now the program adds the tasks to the group without any problems.