【问题标题】:Append result arrays in Realm在 Realm 中追加结果数组
【发布时间】:2017-04-24 04:44:36
【问题描述】:

大家早上好,

我有一个表格视图,它显示从 2 个不同类保存的对象。我要将这些结果组合成一个数组以显示在 tableView 中。这是我的代码:

import UIKit
import RealmSwift

class BookmarksVC: UIViewController,UITableViewDelegate,UITableViewDataSource {

var articleList: Results<DynamicObject>!
var documentList: Results<DynamicObject>!
var list = [Any]()
var article: Any!
var searchBar:UISearchBar = UISearchBar(frame: CGRect(x: 0, y: 0, width: 280, height: 20))

@IBOutlet weak var tableView: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()
    tableView.register(UINib(nibName: "BookmarkCell", bundle: nil), forCellReuseIdentifier: "bookmarkCell")
    tableView.delegate = self
    tableView.dataSource = self
    drawNavBarUI(navigationItem: self.navigationItem, searchBar: searchBar)
    loadDataAndUpdateUI()
}

func loadDataAndUpdateUI() {
    articleList = realm.dynamicObjects(NewsArticle.className())
    documentList = realm.dynamicObjects(Documents.className())
    list.append(articleList)
    list.append(documentList)
    tableView.setEditing(false, animated: true)
    tableView.reloadData()
}

但结果是:

它导致 tableview 只显示了 2 个单元格。我尝试使用 append(ContentsOf:) 但 Xcode 将其强制返回到 append()。这是我第一次使用Realm,所以我可能对它没有深入的了解。所以任何人都可以帮我解决这个问题吗?谢谢大家的阅读,对我的英语不好感到抱歉。

【问题讨论】:

    标签: ios swift3 append nsarray realm


    【解决方案1】:

    来自Realm的Katsumi。我们不建议将Results 对象复制到Array。它失去了Results 的有用功能,例如自动更新、延迟加载等。此外,使用DynamicObject 也会失去类型安全性。

    即使您有多个Results 对象,您也可以直接使用Results 作为数据源。像下面这样:

    class BookmarksVC: UIViewController,UITableViewDelegate,UITableViewDataSource {
        var articleList: Results<NewsArticle>!
        var documentList: Results<Documents>!
    
        @IBOutlet weak var tableView: UITableView!
    
        override func viewDidLoad() {
            super.viewDidLoad()
            ...
    
            loadDataAndUpdateUI()
        }
    
        func loadDataAndUpdateUI() {
            articleList = realm.objects(NewsArticle.self)
            documentList = realm.objects(Documents.self)
    
            ...
    
            tableView.reloadData()
        }
    
    
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return articleList.count + documentList.count
        }
    
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
            if indexPath.row < articleList.count {
                let article = articleList[indexPath.row]
                let cell = tableView.dequeueReusableCell(withIdentifier: "bookmarkCell", for: indexPath) as! BookmarkCell
                cell.configureCell(article: article)
                return cell
            } else {
                let document = documentList[indexPath.row - articleList.count]
                ...
            }
        }
    
        ...
    
    }
    

    【讨论】:

    • 嗨@kishikawa-katsumi,使用领域列表而不是结果是否也会失去自动更新和延迟加载?我想对我的结果进行一些改组,这是不可能的。 List 会为我提供更多的灵活性,但我担心会失去 Results 的潜力。提前致谢!
    【解决方案2】:

    首先,您不应该使用任意数组。导致问题的原因是 Swift 将 Results 集合转换为单个 Any 对象。 您应该使用 append(contentsOf: ),因为这是接受序列作为输入而不仅仅是单个元素的函数。 由于您只想将 Realm 对象存储在列表中,请将其更改为 DynamicObjects 的 Realm 列表,并在将结果集合转换为列表后调用 append。

    var list = List<DynamicObjects>()
    list.append(contentsOf: List(articleList))
    list.append(contentsOf: List(documentList))
    

    【讨论】:

    • 感谢您的帮助,我试过了,但它返回异常如下:“对象类型‘NewsArticle’与 RLMArray 类型‘DynamicObject’不匹配”。我应该在保存操作中进行编辑吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多