Skip to content

Sort dataSource after get realtimedatabase #169

@famonsei

Description

@famonsei

Step 2: Describe your environment

  • Objective C or Swift: Swift
  • iOS version: 8.4
  • Firebase SDK version: 3.7.0, Core 3.4.3, Database 3.0.3
  • FirebaseUI version: 0.5.5
  • CocoaPods Version: 1.0.1

Step 3: Describe the problem:

I need to sort tableview according user default preferences like favorite
So the data favorite not save on my real database
How can I sort the datasource ? maybe from populateCellWithBlock (I use it to bod the favorite item) ?

Observed Results:

  • No sort by favorite

Expected Results:

  • No sort by favorite

Relevant Code:

  override func viewDidLoad() {
        super.viewDidLoad()

        // Firebase database
        self.ref = FIRDatabase.database().reference()
        self.dataSource = FirebaseTableViewDataSource(query: self.ref.child("libraries"),
                                                      prototypeReuseIdentifier: self.libraryCellIdentifier,
                                                      view: self.mTableView)
        // I understand that override numberOfRowsInSection and cellForRowAtIndexPath
        self.dataSource!.populateCellWithBlock { (cell: UITableViewCell, obj: NSObject) -> Void in
            let libraryCell = cell as! LibraryCell
            let snap = obj as! FIRDataSnapshot
            let library = Library(snapshot: snap)
            libraryCell.mLibraryNameLabel.text = library.name
            // changed font works
            if self.librariesFavorite.contains(String(library.id)) {
                library.favorite = true
                libraryCell.mLibraryNameLabel.font = UIFont.boldSystemFontOfSize(15.0)
            } else {
                library.favorite = false
                libraryCell.mLibraryNameLabel.font = UIFont.systemFontOfSize(15.0)
            }
            self.libraries.append(library)
        }

        self.mTableView.dataSource = dataSource
        self.mTableView.delegate = self
    }

override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        // refresh with new favorite data when your come from detail library
        self.librariesFavorite = Helpers.getFavorite("Libraries")
        // sort by favorite before load
//        self.libraries.sortInPlace({$0.favorite && !$1.favorite})
        self.mTableView.reloadData()
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions