Parse LiveQuery How do I uses Loadmore , limits

118 Views Asked by At

Hi I'm uses ParseLiveQuery and PFSubclassing in Swift 3

My question is limits is not working for me.

Here is my PFSubclassing

import Foundation
import Parse

class Post: PFObject, PFSubclassing {

    @NSManaged var postBy: PFUser?
    @NSManaged var postText: String?
    @NSManaged var postImg: PFFile?
    @NSManaged var sell: NSNumber?
    @NSManaged var commentPointer: PFObject?
    @NSManaged var commentBy: PFUser?
    @NSManaged var comment: String?
    @NSManaged var likeCount: NSNumber?
    @NSManaged var mention: NSArray?
    @NSManaged var hashtag: NSArray?

    static func parseClassName() -> String {
        return "Post"
    }


    override class func query() -> PFQuery<PFObject>? {

        let query = PFQuery(className: Post.parseClassName())

        query.whereKeyExists("objectId")
        query.includeKeys(["postBy", "commentPointer", "commentPointer.commentBy", "commentBy"])
        query.order(byDescending: "createdAt")
        query.cachePolicy = .networkElseCache
        return query
    }

}

And It is my tableVieController

class FeedVC: UITableViewController, ShopDetailDelegate {

    private var subscription: Subscription<Post>!

    //Add friends and me

    var postLiveQuery: PFQuery<Post> {
        return Post.query() as! PFQuery<Post>
    }


    var client: Client {

        return ParseLiveQuery.Client.shared
    }

    var results = [Post]()

I had declare subscription in ViewDidLoad()

   subscription = client.subscribe(postLiveQuery).handleSubscribe { [weak self]  (_) in
            // Fetch the objects on subscription to not miss any
            self?.fetchObjects()
            }.handleEvent { [weak self] (_, event) in
                self?.handleEvent(event: event)
        }

And Here is what I'm trying to this.

func fetchObjects() {
        postLiveQuery.limit = self.page
        postLiveQuery.findObjectsInBackground().continue(with: BFExecutor.mainThread(), with: { (task) -> Any? in
            guard let objects = task.result as? [Post] else {
                return nil
            }
            self.results = objects
            self.tableView.reloadData()

            print("Got it!!!")

            print(objects)
            return nil
        })
    }

    //load more
    func morefetchObjects() {
        if page <= results.count {

            //Start animating indicator
            indicator.startAnimating()

            //increase page size to load + 10 posts
            page = page + 1

            postLiveQuery.limit = self.page
            postLiveQuery.findObjectsInBackground().continue(with: BFExecutor.mainThread(), with: { (task) -> Any? in
                guard let objects = task.result as? [Post] else {
                    return nil
                }
                self.results = objects
                self.tableView.reloadData()

                print("morefetchObjects")

                print(objects)
                return nil
            })


        }

    }



    //scrolled down
    override func scrollViewDidScroll(_ scrollView: UIScrollView) {

        if scrollView.contentOffset.y >= scrollView.contentSize.height - self.view.frame.size.height * 2 {

                morefetchObjects()
        }
    }

It works fine but When I called fetchObject() then It retrieve all of datas from parse-server. I want fetch only limits and then When I scroll down I wish to get retrieve more data.

How can I implement this?

0

There are 0 best solutions below