Page break when scrolling in Swift Firebase

I am loading data from a Firebase database. I am trying to load data while scrolling user. For example, if it scrolls around 20 cells, then I want to load a little over 5 cells. I am trying to achieve this, but this does not work:

func parseSnusBrands(){
        let ref = FIRDatabase.database().reference().child("Snuses").child("Brands")

        ref.queryOrderedByKey().queryLimitedToLast(20).observeEventType(.Value, withBlock: { (snapshot) in
            if snapshot.exists() {
                if let all = (snapshot.value?.allKeys)! as? [String]{
                    self.snusBrandsArray = all
                    self.snusBrandsTableView.reloadData()

                }
            }
        })
    }

and determine how many cells scroll:

override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
    if indexPath.row == 20 {
        let ref = FIRDatabase.database().reference().child("Snuses").child("Brands")

        ref.queryOrderedByKey().queryLimitedToLast(20).observeEventType(.Value, withBlock: { (snapshot) in
            if snapshot.exists() {
                if let all = (snapshot.value?.allKeys)! as? [String]{
                    self.snusBrandsArray = all
                    self.snusBrandsTableView.reloadData()

                }
            }
        })
    }
}

This is my Firebase framework :

enter image description here

Is there a better solution or should I keep trying this?

Here you can see the whole code. I know that he lacks OO, but I'm learning :)

+1
source share
1 answer

offset .

order , , .

,

"brands": {
         "catch" : {
           ...
           ...
           "pOrder" : 555
         },
         "etan" : {
           ...
           ...
           "pOrder" : 444
         },
         "general" : {
           ...
           ...
           "pOrder" : 555
         }.
         .....
       }

21 , 20 , , offset, .

, firebase:

 // MARK: Retrieving Data: Firebase
    /*
     retrieve current set of posts sorted by updated time of posts
     */
    func retrievePost(offset: NSNumber, callFlag: Bool, completion: (result: AnyObject?, error: NSError?)->()){
        // As this method is called from viewDidLoad and fetches 20 records at first.
        // Later when user scrolls down to bottom, its called again
        let postsRef = ref.child(kDBPostRef)
        var startingValue:AnyObject?
        // starting  value will be nil when this method is called from viewDidLoad as the offset is not set

        if callFlag{
            if offset == 0{
                startingValue = nil
            }
            else{
                startingValue = offset
            }
        } else{
            // get offset from the offsetArray
            startingValue = self.findOffsetFromArray()

        }
        // sort records by pOrder fetch offset+1 records
        self.refHandler = postsRef.queryOrderedByChild("pOrder").queryStartingAtValue(startingValue).queryLimitedToFirst(kPostLimit + 1).observeEventType(FIRDataEventType.Value, withBlock: { (snapshot) in
            // flag is for setting the last record/ 21st as offset
            var flag = 0

            let tempPost = NSMutableSet()
                // iterate over children and add to tempPost
                for item in snapshot.children {

                    // check for offet, the last row(21st) is offset ; Do not add last element in the main table list
                    flag += 1
                    if flag == 21 && callFlag == true{
                        // this row is offset
                        self.kOffset = item.value?["pOrder"] as! NSNumber
                        self.offSetArray?.append(self.kOffset)
                        continue
                    }
                    // create Post object
                    let post = Post(snapshot: item as! FIRDataSnapshot)

                    // append to tempPost
                    tempPost.addObject(post)
                }
                // return to the closure
                completion(result:tempPost, error:nil)
        })
    }

updateNewRecords viewDidLoad, 0 20 .

 func updateNewRecords(offset:NSNumber, callFlag: Bool){
        self.retrievePost(offset,callFlag:callFlag) { (result,error) -> Void in
//            let tempArray = result as! [Post]
            let oldSet = Set(self.posts)
            var unionSet = oldSet.union(result as! Set<Post>)
            unionSet = unionSet.union(unionSet)
            self.posts = Array(unionSet)
            self.postsCopy = self.posts
//          print(self.posts.count)
            self.posts.sortInPlace({ $0.pOrder> $1.pOrder})
            self.reloadTableData()
        }
    }

, table view, , updateNewRecords , 20 .

func scrollViewDidEndDragging(scrollView: UIScrollView, willDecelerate decelerate: Bool) {

        // UITableView only moves in one direction, y axis
        let currentOffset = scrollView.contentOffset.y
        let maximumOffset = scrollView.contentSize.height - scrollView.frame.size.height

        // Change 10.0 to adjust the distance from bottom
        if maximumOffset - currentOffset <= 10.0 {
            self.updateNewRecords(self.kOffset, callFlag:true)
        }
    } 

offsetArray , firebase.

, , - , observeEventType, firebase. , offsets. , - , retrievePost offset offsetArray. ( viewDidLoad scrollViewDidEndDragging) kOffset, ( ) offsetArray.

, offset :

// find the offset from the offsetDict
    func findOffsetFromArray() -> NSNumber{
        let idx = self.kClickedRow/20 // kClickedRow is the updated row in the table view
        return self.offSetArray![idx]

    }

retrievePost .

+2

Source: https://habr.com/ru/post/1665303/


All Articles