1

Good morning everybody,

I'm having a table view which display objects saved from 2 different classes. Im going to combine these result into one array to display in tableView. Here is my code:

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()
}

But the result was:

enter image description here

it lead to the tableview just displayed only 2 cell. I tried to use append(ContentsOf:) but Xcode forced it back to append(). This is my first time using Realm, so i might not have deep understanding about it. So anyone can help me to solve this problem?. Thank you guys for reading and sorry for my bad English.

2 Answers 2

8

Katsumi from Realm here. We don't recommend to copy Results objects to Array. It loses the useful ability of Results such like auto-updating, lazy loading etc. Also, using DynamicObject loses type safety.

You can use Results as data source directly even if you have multiple Results objects. Like the following:

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]
            ...
        }
    }

    ...

}
Sign up to request clarification or add additional context in comments.

1 Comment

Hi @kishikawa-katsumi , would one also lose the auto-updating & lazy loading using a realm List instead of Results? I'd like to perform some shuffling on my Results and that's not possible. List would offer me more flexibility, but I'm concerned about losing the potentials of Results. Thanks in advance!
0

First of all, you shouldn't use an Array of Any. The problem is caused, because Swift converts the Results collection to a single Any object. You should use append(contentsOf: ), since this is the function that accepts a sequence as an input and not just a single element. Since you only want to store Realm objects in list, change it to a Realm List of DynamicObjects and call append after converting the Results collection to a List.

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

1 Comment

Thanks for your help, i tried but it return the exception as follow: "Object type 'NewsArticle' does not match RLMArray type 'DynamicObject". Should I edit sth in the saving action?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.