简体   繁体   中英

Realm init for storyboard tableview

I'm having a problem implementing a synced realm with a storyboard ViewController.

Specifically I'm unsure how to provide the initializers.

If I create the ViewController programmatically I can use the following and everything works fine. However, I'm working with a storyboard. This code is provided by the realm tutorial.

import UIKit
import RealmSwift

class ItemsViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

let realm: Realm
let items: Results<Item>

let refresh = UIRefreshControl()

let tableView = UITableView()
var notificationToken: NotificationToken?

override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
    let syncConfig = SyncConfiguration(user: SyncUser.current!, realmURL: Constants.REALM_URL)
    self.realm = try! Realm(configuration: Realm.Configuration(syncConfiguration: syncConfig, objectTypes:[Item.self]))
    self.items = realm.objects(Item.self).sorted(byKeyPath: "body", ascending: true)

    super.init(nibName: nil, bundle: nil)
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

deinit {
    notificationToken?.invalidate()
}

override func viewDidLoad() {
    super.viewDidLoad()

    title = "Items to Get Done"
    tableView.dataSource = self
    tableView.delegate = self
    view.addSubview(tableView)
    tableView.frame = self.view.frame

....

This does not work for a storyboard ViewConroller since (as I understand) the storyboard is configuring and initializing the ViewConroller.

My question then is, how and where do I write the init?

You can write realm loading in your ViewDidLoad.

        import UIKit
        import RealmSwift

        class ItemTableViewController: UITableViewController {

            var realm!
            var items: Results<Item>?

            override func viewDidLoad() {
                super.viewDidLoad()
                self.loadItems()
            }


            //Readcategory:
            func loadItems() {
                let syncConfig = SyncConfiguration(user: SyncUser.current!, realmURL: Constants.REALM_URL)
                self.realm = try! Realm(configuration: Realm.Configuration(syncConfiguration: syncConfig, objectTypes:[Item.self]))
                self.items = realm.objects(Item.self).sorted(byKeyPath: "body", ascending: true)
                self.tableView?.reloadData()
            }

            // MARK: - Table view data source
            override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
                return self.items?.count
            }

            override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

                let cell = super.tableView(tableView, cellForRowAt: indexPath) as! SwipeCell
                if let item = self.items?[indexPath.row] {
                    cell.textLabel?.text = item.title

                }
                return cell
            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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