Realm.io' RLMArray不再有效'删除具有子对象的RLMObject时

时间:2015-02-21 23:27:35

标签: swift realm

在我的应用中,我一直在接收,' RLMException',原因:' RLMArray不再有效'尝试删除包含与另一个RLMObject的一对多关系的RLMObject时。例如:'任务'是RLMObject,它包含一个RLMArray'记录'类型'记录' RLMObjects在其中。抛出错误的代码如下:

public class func deleteTask(#taskName: String, retainRecords: Bool) {
    let realm = Database.getRealm()
    let currentTask = (Task.objectsWhere("name = '\(taskName)'").objectAtIndex(0) as Task)
    let loopCount:UInt = currentTask.records.count

    if (retainRecords) {
        for var i:UInt = 0; i < loopCount; ++i {
            Database.moveRecord(record: (currentTask.records.objectAtIndex(0) as Record), newTask: "Taskless Records")
        }
    } else {
        for var i:UInt = 0; i < loopCount; ++i {
            Database.deleteRecord(record: currentTask.records.objectAtIndex(0) as Record)
        }
    }

    realm.beginWriteTransaction()
    realm.deleteObject(currentTask)
    realm.commitWriteTransaction()
}

该程序在该行上抛出异常,

realm.commitWriteTransaction()

在尝试删除任务之前,循环正在移动或删除任务下的所有子数据库对象。移动和删除记录都可以正常工作。还有最后一个重要注意事项,如果任务不包含其下的记录,则删除它并没有任何问题,也不会抛出任何异常。

我感谢任何人的帮助,我一直在为这个问题敲打砖头。


- 基于yoshyosh的回复的新变化 -

我已将代码修改为以下内容。我仍然收到同样的错误。

public class func deleteTask(#taskName: String, retainRecords: Bool) {
    let realm = Database.getRealm()
    let currentTask = (Task.objectsWhere("name = '\(taskName)'").firstObject() as Task)
    let currentRecords = currentTask.records
    let parent = currentTask.parent

    if (retainRecords) {
        let loopCount:UInt = currentTask.records.count

        for var i:UInt = 0; i < loopCount; ++i {
            Database.moveRecord(record: (currentTask.records.firstObject() as Record), newTaskName: "Taskless Records")
        }
    } else {
        realm.transactionWithBlock { () -> Void in
            realm.deleteObjects(currentRecords)
        }
    }

    realm.transactionWithBlock { () -> Void in
        realm.deleteObject(currentTask)
    }
}

public class func moveRecord(#record: Record, newTaskName: String) {
    let realm = Database.getRealm()
    let oldTask = record.parent
    let oldIndex = oldTask.records.indexOfObject(record)
    let newTask = (Task.objectsWhere("name = '\(newTaskName)'")).firstObject() as Task

    realm.transactionWithBlock { () -> Void in
        oldTask.records.removeObjectAtIndex(oldIndex)
        newTask.records.addObject(record)
    }
}

public class func addTask(#name: String, memo: String, time: Double, categoryName: String) {
    let realm = Database.getRealm()
    let parentCategory = (Category.objectsWhere("name = '\(categoryName)'")).firstObject() as Category

    realm.transactionWithBlock { () -> Void in
        let newTask = Task()

        newTask.parent = parentCategory
        newTask.name = name
        newTask.memo = memo
        newTask.timeRemaining = time

        parentCategory.tasks.addObject(newTask)
    }     
}

public class func addRecord(#taskName: String, note: String, timeSpent: Double, date: NSDate) {
    let realm = Database.getRealm();

    let parentTask = Task.objectsWhere("name = '\(taskName)'").firstObject() as Task
    realm.transactionWithBlock { () -> Void in
        let newRecord = Record()

        newRecord.parent = parentTask
        newRecord.note = note
        newRecord.timeSpent = timeSpent
        newRecord.date = date

        parentTask.records.addObject(newRecord)
    }
}

如果需要,我还可以添加用于向数据库添加新记录和新任务的代码。 Yoshyosh我感谢您的评论,因为它确实帮我清理了我的代码,不幸的是,删除任务时仍然会抛出错误。

我在测试应用时注意到的一件奇怪的事情是,当我尝试删除任务时它会崩溃,但是一旦我再次打开应用程序,任务和所有记录都会被删除。


- 解决方案 -

我发现我正在寻找错误的完全错误的地方。它是由一个TableView视图控制器引起的,该控制器填充了传递给它的任务的记录。它使用RLMArray从任务中检索recordList,并使用notificationToken重新加载tableView数据。

var currentTask:Task!
var recordList:RLMArray!
var notificationToken: RLMNotificationToken?

override func viewDidLoad() {
    super.viewDidLoad()

    recordList = currentTask.records

    notificationToken = RLMRealm.defaultRealm().addNotificationBlock { note, realm in
        self.tableView.reloadData()
    }
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return Int(self.recordList.count)
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    return Factory.prepareRecordCell(tableView: tableView, recordList: self.recordList, indexPath: indexPath)
}

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

当我从单独的视图中删除任务时,它会尝试使用&#39; recordsList&#39;重新加载数据。 RLMArray删除了一个任务。

删除notificationToken并在viewWillAppear()函数中手动重新加载tableView数据后,删除任务可以正常运行。

1 个答案:

答案 0 :(得分:2)

我不确定您的具体情况,您需要在deleteRecord发生的地方显示更多代码。我试图尽可能地重现你的情况,但是使用下面的代码删除了所有内容:

    let currentTask = Task.allObjects().firstObject() as Task
    let currentRecords = currentTask.records
    let loopCount = currentRecords.count

    for var i:UInt = 0; i < loopCount; i++ {
        realm.transactionWithBlock({ () -> Void in
            var recordToDelete = currentTask.records.objectAtIndex(0) as Record
            realm.deleteObject(recordToDelete)
        })
    }

    realm.transactionWithBlock { () -> Void in
        realm.deleteObject(currentTask)
    }

我认为删除所有相关对象的更简单方法是这样的:

    let currentTask = Task.allObjects().firstObject() as Task
    let currentRecords = currentTask.records

    // Delete Task and records that are connected to it
    realm.transactionWithBlock { () -> Void in
        realm.deleteObjects(currentRecords)
        realm.deleteObject(currentTask)
    }

这是使用其他记录更新任务的简单方法。此代码缺少您更新这些记录的父级的部分

let currentTask = Task.objectsWhere("name = %@", "First task").firstObject() as Task
let currentRecords = currentTask.records
let newFetchedTask = Task.objectsWhere("name = %@", "New Task").firstObject() as Task
realm.transactionWithBlock { () -> Void in
    newFetchedTask.records.addObjects(currentRecords)
    currentTask.records.removeAllObjects()
}

随意发布更多代码以调试您的特定案例