简体   繁体   English

Magical Record 删除所有记录 Swift

[英]Magical Record delete all records Swift

I'm trying to clear all rows from a table in Magical Record in Swift.我正在尝试清除 Swift 中 Magical Record 表中的所有行。 I tried:我试过了:

DeviceLog.mr_truncateAll() 

but this seems to have no effect.但这似乎没有效果。 Does someone know how to do it properly?有人知道如何正确地做到这一点吗?

Thanks in advance.提前致谢。

Looks like I wasn't awake, it worked when I do it in save.看起来我没有醒,当我保存时它起作用了。

MagicalRecord.save({ (context) in
            DeviceLog.mr_truncateAll(in: context)
        }, completion: { (changed, error) in
            if let _ = error {
                print("Error truncating DeviceLog: \(String(describing: error?.localizedDescription))")
            } else {
                print("Truncate DeviceLog successful: \(changed)")
                print(DeviceLog.mr_countOfEntities())
            }
        })

Or you could just use this或者你可以使用这个

DeviceLog.mr_truncateAll(in: context)
context.mr_saveToPersistentStoreAndWait()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM