Skip to content

Data/File Paging

Data paging in Backendless 3 is built into the collection returned from the data retrieval operations. Version 5 changes the mechanism by shifting paging operations to the data query class. See the examples below:

Version 3.x

Consider the following data table in Backendless:

sample-data-paging.zoom50
The code for retrieving the data using default page size (10 objects):

- (void)paging {
    QueryOptions *queryOptions = [QueryOptions new];
    [queryOptions setPageSize:@10];
    BackendlessDataQuery *dataQuery = [BackendlessDataQuery new];
    dataQuery.queryOptions = queryOptions;

    [[backendless.data of:[Restaurant class]] find:dataQuery response:^(BackendlessCollection *restaurants) {
        if ((int)restaurants.getCurrentPage.count > 0) {
            NSLog(@"Loaded %i restaurants in the current page", (int)restaurants.getCurrentPage.count);
            for (Restaurant *restaurant in restaurants.getCurrentPage) {
                NSLog(@"%@", restaurant.name);
            }
        }
        [self nextPage:restaurants];
    } error:^(Fault *fault) {
        NSLog(@"Error: %@", fault.message);
    }];
}

- (void)nextPage:(BackendlessCollection *)restaurantCollection {
    if ([restaurantCollection getCurrentPage].count == 0) {
        NSLog(@"Reached the end of collection");
        return;
    }
    [restaurantCollection nextPageAsync:^(BackendlessCollection *restaurants) {
        if((int)restaurants.getCurrentPage.count > 0) {
            NSLog(@"Loaded %i restaurants in the current page", (int)restaurants.getCurrentPage.count);
            for (Restaurant *restaurant in restaurants.getCurrentPage) {
                NSLog(@"%@", restaurant.name);
            }
        }
        [self nextPage:restaurants];
    } error:^(Fault *fault) {
        NSLog(@"Error: %@", fault.message);
    }];
}
func paging() {
    let queryOptions = QueryOptions()
    queryOptions.pageSize = 10
    let dataQuery = BackendlessDataQuery()
    dataQuery.queryOptions = queryOptions

    Backendless.sharedInstance()?.data.of(Restaurant.self)?.find(dataQuery, response: { restaurants in
        if let restaurants = restaurants?.getCurrentPage() as? [Restaurant], restaurants.count > 0 {
            print("Loaded \(restaurants.count) restaurants in the current page")
            for restaurant in restaurants {
                print("\(restaurant.name ?? "")")
            }
        }
        self.nextPage(restaurantCollection: restaurants)
    }, error: { fault in
        print("Error: \(fault?.message ?? "")")
    })
}

func nextPage(restaurantCollection: BackendlessCollection?) {
    if restaurantCollection?.getCurrentPage()?.count == 0 {
        print("Reached the end of collection")
        return
    }
    restaurantCollection?.nextPageAsync({ restaurants in
        if let restaurants = restaurants?.getCurrentPage() as? [Restaurant], restaurants.count > 0 {
            print("Loaded \(restaurants.count) restaurants in the current page")
            for restaurant in restaurants {
                print("\(restaurant.name ?? "")")
            }
        }
        self.nextPage(restaurantCollection: restaurants)
    }, error: { fault in
        print("Error: \(fault?.message ?? "")")
    })
}

The code produces the following output:

Loaded 10 restaurants in the current page  
  Artin's Grill  
  Istanbul  
  Magic Wok  
  Olive Garden  
  Platia  
  Cantina Loredo  
  Wendy's  
  Chili's  
  Kenny's Burgers  
  Tony Roma's  
Loaded 1 restaurants in the current page  
  McDonnald's  
Reached the end of collection

Version 5.x

Identical to the screenshot from 3.x above, consider the following data in version 5 of Backendless:

- (void)paging {
    [[Backendless.shared.data of:[Restaurant class]] findWithResponseHandler:^(NSArray *restaurants) {
        if ((int)restaurants.count > 0) {
            NSLog(@"Loaded %i restaurants in the current page", (int)restaurants.count);
            for (Restaurant *restaurant in restaurants) {
                NSLog(@"%@", restaurant.name);
            }
        }
        DataQueryBuilder *queryBuilder = [DataQueryBuilder new];
        [queryBuilder prepareNextPage];
        [self nextPage:queryBuilder];
    } errorHandler:^(Fault *fault) {
        NSLog(@"Error: %@", fault.message);
    }];
}

- (void)nextPage:(DataQueryBuilder *)queryBuilder {
    [[Backendless.shared.data of:[Restaurant class]] findWithQueryBuilder:queryBuilder responseHandler:^(NSArray *restaurants) {
        if ((int)restaurants.count == 0) {
            NSLog(@"Reached the end of collection");
            return;
        }
        NSLog(@"Loaded %i restaurants in the current page", (int)restaurants.count);
        for (Restaurant *restaurant in restaurants) {
            NSLog(@"%@", restaurant.name);
        }
        [queryBuilder prepareNextPage];
        [self nextPage:queryBuilder];
    } errorHandler:^(Fault *fault) {
        NSLog(@"Error: %@", fault.message);
    }];
}
func paging() {
    Backendless.shared.data.of(Restaurant.self).find(responseHandler: { restaurants in
        if let restaurants = restaurants as? [Restaurant],
            restaurants.count > 0 {
            print("Loaded \(restaurants.count) restaurants in the current page")
            for restaurant in restaurants {
                print("\(restaurant.name ?? "")")
            }
        }
        let queryBuilder = DataQueryBuilder()
        queryBuilder.prepareNextPage()
        self.nextPage(queryBuilder: queryBuilder)
    }, errorHandler: { fault in
        print("Error: \(fault.message ?? "")")
    })
}

func nextPage(queryBuilder: DataQueryBuilder) {
    Backendless.shared.data.of(Restaurant.self).find(queryBuilder: queryBuilder, responseHandler: { restaurants in
        if restaurants.count == 0 {
            print("Reached the end of collection")
            return
        }
        if let restaurants = restaurants as? [Restaurant] {
            print("Loaded \(restaurants.count) restaurants in the current page")
            for restaurant in restaurants {
                print("\(restaurant.name ?? "")")
            }
            queryBuilder.prepareNextPage()
            self.nextPage(queryBuilder: queryBuilder)
        }
    }, errorHandler: { fault in
        print("Error: \(fault.message ?? "")")
    })
}

The code produces the same output as the code for 3.x