gpt4 book ai didi

iOS RealmSwift 存储与 Swift 4

转载 作者:行者123 更新时间:2023-11-30 11:41:42 26 4
gpt4 key购买 nike

我正在尝试使用 RealmSwift 将项目保存到 Swift 4 中的手机存储中。我有两个不同的 View ;一个用于保存功能,另一个将所有保存的项目显示到 TableView 中。我有一个可构建的表单编码,但当我调用 realm.add 时,我抛出了一个错误 Thread 1: signal SIGABRT 。当我在保存 View 中时,我使用带有按钮的 IBAction 来启动保存功能。谁能帮我解决这个问题吗?我认为问题是当我设置 Realm 的变量时,但我不确定。

<小时/>

更新:

我已经更改了我的实现,以反射(reflect)该线程中关于我的原始问题的想法。执行此操作后,当调用将项目添加到 Realm 时,我会在 API 源代码内崩溃 EXC_BAD_ACCESS (code=EXC_I386_GPFLT)。具体来说,我在 API 的这个函数处崩溃了

//CODE EXCERPT FROM REALMSWIFT API 

// Property value from an instance of this object type
id value;
if ([obj isKindOfClass:_info.rlmObjectSchema.objectClass] &&
prop.swiftIvar) {
if (prop.array) {
return static_cast<RLMListBase *>(object_getIvar(obj,
prop.swiftIvar))._rlmArray;
}
else { // optional
value = static_cast<RLMOptionalBase *>(object_getIvar(obj,
prop.swiftIvar)).underlyingValue; //CRASH OCCURS HERE!!!!!!!!
}
}
else {
// Property value from some object that's KVC-compatible
value = RLMValidatedValueForProperty(obj, [obj
respondsToSelector:prop.getterSel] ? prop.getterName : prop.name,

_info.rlmObjectSchema.className);
}
return value ?: NSNull.null;
<小时/>
import UIKit
import RealmSwift

class DetailsViewController: UIViewController {

var titleOfBook: String?
var author: String?

@IBAction func SavetoFavorites(_ sender: Any) {
DispatchQueue.global().async { [weak self] in
guard let strongSelf = self else { return }
guard let realm = try? Realm() else {
return
}

let newItem = Favorites()
newItem.title = strongSelf.titleOfBook
newItem.author = strongSelf.author

try? realm.write {
realm.add(newItem) // Crashes on this line
}
}
}
<小时/>
import UIKit
import RealmSwift

final class Favorites: Object {
var title: String?
var author: String?
}

class FavoritesTableViewController: UITableViewController {

var items: Array<Favorites> = []

override func viewDidLoad() {
super.viewDidLoad()
tableView.register(UITableViewCell.self, forCellReuseIdentifier:
"cell")
}

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
tableView.reloadData()
}


override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}

override func numberOfSections(in tableView: UITableView) -> Int {
return 0
}

override func tableView(_ tableView: UITableView?,
numberOfRowsInSection section: Int) -> Int {
return items.count
}

override func tableView(_ tableView: UITableView, cellForRowAt
indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell",
for: indexPath)
let item = items[indexPath.row]
cell.textLabel?.text = item.title
cell.detailTextLabel?.text = item.author
return cell
}

var selectedIndexPath: NSIndexPath = NSIndexPath()

override func tableView(_ tableView: UITableView, willSelectRowAt
indexPath: IndexPath) -> IndexPath? {
selectedIndexPath = indexPath as NSIndexPath
return indexPath
}

最佳答案

您必须将 realm.add(newItem) 包装在事务内:

try! realm.write {
realm.add(newItem)
}

请注意,写事务会阻塞它们所在的线程,因此如果您要写入大部分数据,您应该在后台线程上执行此操作( Realm 也必须在该线程上实例化)。你可以这样做:

@IBAction func saveToFavorites(_ sender: Any) {
DispatchQueue.global().async { [weak self] in
guard let strongSelf = self else { return }
guard let realm = try? Realm() else {
// avoid force unwrap, optionally report an error
return
}

let newItem = Favorites()
newItem.title = strongSelf.titleOfBook
newItem.author = strongSelf.author

try? realm.write {
realm.add(newItem)
}
}
}

更新:我还没有注意到您的模型也有问题 - 由于 Realm 是用 Objective C 编写的,您应该使用 @objcdynamic 修饰符标记您的模型属性:

final class Favorites: Object {
@objc dynamic var title: String?
@objc dynamic var author: String?
}

关于iOS RealmSwift 存储与 Swift 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49225686/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com