This is an implementation of the Java Collection interface enabling to retrieve and iterate over a collection of objects stored in a Backendless data table.
Interface methods that returning data are mapped to various Backendless Data APIs.
The Iterator returned by the implementation lets you access either all objects from the data table or a subset determined by a where clause. Additionally, the implementation can work with data streams.
The collection has two modes of operation:
- persisted mode - all retrieved objects are saved locally to enable faster access in future iterations. The persisted data is shared between all iterators returned by the collection. To enable this mode use the
preserveIteratedData
parameter. - transient mode - every iterator returned by the collection works with a fresh data collection returned from the server.
The collection is not thread safe.
Also be sure you properly mapped your custom type with
Backendless.Data.mapTableToClass( String tableName, Class<T> entityClass )
BackendlessDataCollection<Order> orders;
1. orders = new BackendlessDataCollection<>( Order.class );
2. orders = new BackendlessDataCollection<>( Order.class, "title = 'phone'" );
3. orders = new BackendlessDataCollection<>( Order.class, true );
4. orders = new BackendlessDataCollection<>( Order.class, true, "title = 'phone'" );
Create ordinary collection for table order which reflect all records from it. By default it is created in transient mode, thus no data will be saved locally.
- the total size of objects (table rows) is retrieved on object creation;
- you can iterate through the entire collection;
- every iteration will perfrom calls to the Backendless server;
- all iterators are independent, so you may create the numbers of it and use it simultaneously;
- if the iterator reached the end (wasn't interrupted) the actual collection size is refreshed automatically;
- all
contains
,add
andremove
operations directly perform calls to Backendless server; - method
invalidateState()
forcibly updates collection real size; - method
isPersisted()
always returns false; - methods
isLoaded()
,getPersistedSize()
,populate()
will throw exception, because they are intended only for persisted mode;
Create collection as a slice of data for table order. Will reflect only a subset of data which satisfy argument slice
(in or case it title = 'phone'
).
Main features are the same as in point (1).
- the total size of objects satisfied the slice is retrieved on object creation;
- you can iterate only through the subset of objects;
- all
contains
,add
andremove
operations directly perform calls to Backendless server and would be discarded if the object doesn't match the slice clause;
Create persisted collection for table order (preserveIteratedData
parameter). Some operations would perform locally (without api calls to the server) and thus drastically reduce perform time.
Main features are the same as in point (1).
- collection is lazy, so the data will be loaded only during iteration over it;
- only first iteration will perform calls to the Backendless server, all subsequent iterations will be done locally without any request;
- if the iterator was interrupted, only part of the data would be saved locally and next time iterator firstly will iterate over local data and then (if need) beging make calls to the server;
- method
isPersisted()
always returns true; - when all data from table saved locally,
isLoaded()
returns true; - method
getPersistedSize()
returns the size of locally saved data, it will be equal tosize()
ifisLoaded() == true
- method
invalidateState()
forcibly updates collection real size and clear all locally saved data, so the next iteration will make requests to the server again; - method
populate()
forcibly download all data from the table (so-called greedy initialization), ifisLoaded() == true
it do nothing;
Create persisted collection as a slice for table order.
Combine features from points (2) and (3).
Returns the current size of collection which reflect the row size in the underlying table. If the table was changed on the server side, the local become irrelevant. For this case you may use invalidateState()
method or perform iteration over collection (after iteration size is updated automatically). Never makes api call to Backendless.
Never makes api call to Backendless. Related to size()
method.
Returns where clause for the current collection or null
if it was created without slice.
Returns true if the collection was created with parameter preserveIteratedData == true
, and false otherwise.
Only for persisted mode.
Forcibly populates current collection from the Backendless data table (greedy initialization). If isLoaded() == true
, do nothing. Under the hood it just iterate over remote table.
Only for persisted mode.
Returns true if the data was retrieved from Backendless table in a full (after invocation populate()
method or full iteration).
Only for persisted mode.
Returns the size of inner store. It may differ from size()
return value if the iteration over collectin was interrupted.
For transient mode forcibly updates collection real size. For persisted mode in addition clear all locally saved data, so the next iteration will make requests to the server again.
Remove all data from the underlying table (if the collection was created without slice
) or remove the subset of data specified in the condition (with slice
). Then the method calls invalidateState()
.
Returns object by its objectId
. Takes into account slice (where clause).
If this collection is persisted and fully loaded, than no api-calls will be performed.
It is always greedy operation. Calling this method will retrive all data from the table.
Takes into account only 'entityType' and 'slice', that were set during collection creation.
Always perfrom api calls to Backendless. Even in persisted mode it is necessary to synchronize local state and remote table.
If this collection is persisted and fully loaded, than no api-calls will be performed.
for( Order o : orders )
System.out.println( o.getOrderDate() );
Iterator<Order> orderIterator = orders.iterator();
while( orderIterator.hasNext() )
{
Order o = orderIterator.next();
System.out.println( o.getObjectId() + ", " + o.getTitle() );
}
for( Order o : orders )
{
System.out.println( o.getTitle() );
if (o.getTitle().equals( "ticket" ))
break;
}
List<Set> orderTitles = orders.stream()
.map( Order::getTitle )
.collect( Collectors.toSet() );
Order[] orderArray = orders.toArray();
String allOrders = Arrays.toString(orderArray);
System.out.println( allOrders );