简体   繁体   English

分页与动态内容

[英]Pagination With Dynamic Content

I'm trying to figure out the proper way to paginate results using the Parse SDK for iOS. 我正在尝试使用适用于iOS的Parse SDK进行分页结果的正确方法。 The problem I'm having is that the content I'm trying to paginate has the potential to change frequently. 我遇到的问题是,我要分页的内容可能会经常更改。

For example: 例如:

There is an app with a bunch of objects stored on a server, and the request to get these objects is sorted with newest first and paginated in case there are a lot of objects. 有一个应用程序在服务器上存储了一堆对象,对于获取这些对象的请求将按照最新的顺序进行排序,以防万一有很多对象。 When the user scrolls, as they are approaching the last object on the current page, the next page gets requested. 当用户滚动时,当他们接近当前页面上的最后一个对象时,将请求下一个页面。

Say the user scrolls to page two, then a new object is added to the list of objects, and the user then scrolls up another page. 假设用户滚动到第二页,然后将新对象添加到对象列表中,然后用户向上滚动另一页。 When the next page is requested, they will get a duplicate of the last message on the second page, and since page 0 has already been loaded, the new object won't be displayed. 当请求下一页时,他们将获得第二页上最后一条消息的副本,并且由于已经加载了页面0,因此不会显示新对象。

How can something like this be handled? 怎样处理这样的事情?

How would the implementation change if the objects were sorted oldest first? 如果将对象排在最旧的位置,实现将如何改变?

Thanks in advance for the help. 先谢谢您的帮助。

Edit: request pseudo code 编辑:请求伪代码

- (void)fetchObjectsForPage:(NSUInteger)page completion:(void (^)(NSArray *_Nullable objects, PageInfo *_Nullable pageInfo, NSError *_Nullable error))completion{
    PFQuery *query = [SomeObject query];
    [query orderByAscending:@"updatedAt"];
    [query setLimit:50];
    [query setSkip:50 * page];
    [query findObjectsInBackgroundWithBlock:^{NSArray *_Nullable objects, NSError *_Nullable error){
        ...
        >>> Do error checking and return array along with paging info (total number of objects, page size) or error in completion block.
    }];
}

It's not the request I'm having trouble with, it's figuring out how to properly handle paging in the table when a new object gets added. 这不是我遇到的麻烦,它是在添加新对象时弄清楚如何正确处理表中的分页。

What you can do is the following: 您可以执行以下操作:

  1. Create a query that will return 50 rows (like you did) order by updatedAt 创建一个查询,该查询将按updatedAt返回50行(与您一样)的顺序
  2. When you get result take the last item from the list and save the item updatedAt in some global property 当您得到结果时,请从列表中获取最后一个项目,并将该项目的updateAt保存在某些全局属性中
  3. In the next call do not use setOffset but get the next 50 rows and add another condition of where updatedAt is greater than the updatedAt of your global object. 在下一个调用中,不要使用setOffset,而是获取下50行,并添加另一个条件,其中up​​dateAt大于全局对象的updatedAt。 This way you make sure that you will not have duplicate records in your list 这样,您可以确保列表中没有重复的记录

At the end your code should look like the following: 最后,您的代码应如下所示:

PFQuery *query = [PFQuery queryWithClassName:@"SomeClassName"];
[query orderByAscending:@"updatedAt"];
[query setLimit:50];

if (self.lastItemUpdatedAt){
    [query whereKey:@"updatedAt" greaterThan:self.lastItemUpdatedAt];
}

[query findObjectsInBackgroundWithBlock:^(NSArray * _Nullable objects, NSError * _Nullable error) {

    if (objects.count > 0){
        self.lastItemUpdatedAt = [[objects lastObject] updatedAt];
    }

}];

Now if you use updatedAt there is a chance that you will still get duplicates because let's assume that someone change the object so the updatedAt will also be changed (so it will be greater than the updatedAt that you saved) in this case you can use createdAt instead of updatedAt since createdAt will never be changed 现在,如果您使用updatedAt,仍然有可能会重复,因为假设有人更改了对象,因此updatedAt也将被更改(因此它将大于您保存的updatedAt),在这种情况下,您可以使用createdAt而不是updatedAt因为createdAt将永远不会改变

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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