Realm object is missing all properties except primaryKey

落花浮王杯 提交于 2019-12-13 02:38:21

问题


I have a RealmObject model that my tableViewDataSource depends on. The cellForRowAtIndexPath1 method is able to access the properties fine, but the didSelectRowAtIndexPath gets all empty properties from the Realm object. I'm guessing this has to do with passing persisted Realm objects around but I'm not sure where to fix it.

//viewController...
override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    let realm = try! Realm()
    let books = realm.objects(Book)
    viewModel = BookListViewModel(books: Array(books), onSelection: onSelection, onDeleteFailure: onDeleteFailure)
}
//end viewController

extension BookListViewModel: UITableViewDataSource {
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier(BookTableViewCell.nibName, forIndexPath: indexPath) as! BookTableViewCell
        cell.bind(bookCellViewModels[indexPath.row])

        // NOTE:
        // bookCellViewModels[indexPath.row].book all properties are valid strings
        return cell
    }

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

extension BookListViewModel: UITableViewDelegate {
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        let book = bookCellViewModels[indexPath.row].book

        // NOTE:
        // all book properties are empty string or nil
        onSelection(book)
        tableView.deselectRowAtIndexPath(indexPath, animated: true)
    }
}

回答1:


Your debugger is likely showing you all empty property values for the Realm object, because you're missing the LLDB plugin.

Install Realm Xcode plugin

via Alcatraz

The LLDB plugin comes bundled with the Realm Xcode plugin, which is available on Alcatraz.

  1. Install Alcatraz, if not already installed.

    curl -fsSL https://raw.githubusercontent.com/supermarin/Alcatraz/deploy/Scripts/install.sh | sh
    
  2. Open the Package Manager in Xcode (⇧⌘9 or Windows > Package Manager)

  3. Search for RealmPlugin

  1. Quit and relaunch Xcode to make sure the plugin is loaded

Compile yourself

You can also install the plugin manually by opening plugin/RealmPlugin.xcodeproj contained in the release zip and clicking build.

Install LLDB plugin only

Alternatively, you can install it as advised in the first code comments of the linked scripts or just execute the script below:

mkdir -p ~/Library/Application\ Support/Realm
wget -O ~/Library/Application\ Support/Realm/rlm_lldb.py https://raw.githubusercontent.com/realm/realm-cocoa/master/plugin/rlm_lldb.py
touch ~/.lldbinit
grep -q "rlm_lldb.py" ~/.lldbinit || echo "command script import "~/Library/Application Support/Realm/rlm_lldb.py" --allow-reload" >> .lldbinit



回答2:


Nevermind, it turned out to be a bug related to my view logic. I just was confused because the Xcode debugger shows all empty property values for the realm object, probably because they're all dynamic properties.



来源:https://stackoverflow.com/questions/33806204/realm-object-is-missing-all-properties-except-primarykey

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!