Sunday, May 2, 2010

Difference between local & remote queries

The difference between local & remote queries may look obvious from their names; local queries query on the local store vs. remote queries query remotely on the backend via the DataSource. But that is not the case.

Both Local & Remote queries have the following in common:

1. Contact the DataSource on the first find()
2. Don't contact to the DataSource on subsequent "find"s on the same query object
3. Contact the DataSource when refresh() is called on the RecordArray returned from a previous find() on the query

The 2 types of queries differ in - "How is the list of data in the results determined?"

Remote queries need the DataSource to tell it the exact list of storeKeys that represent result of the query. The DataSource usually does this by calling store.loadQueryResults(query, listOfStoreKeys).

Even though a new record has already been loaded into the store using store.loadRecord(), that record's storeKey needs to be explicitly included in the list passed to loadQueryResults(). Otherwise, it will not show up in the query results.

Local queries look at the store for all objects matching the given conditions and automatically include them into the query results.

It does not matter if a record was loaded into the store in response to this local query or due to some other remote query that got fired. If the record matches the conditions of the local query, it gets included into the results.

Understanding this difference is key to deciding when & how to use local vs. remote queries.

Since local queries also fetch data from the backend via the DataSource, we can then argue about the need for remote queries at all. There may be many cases where remote queries are useful e.g. the backend decides on the list of results for the query based on a hidden attribute visible only to the backend (hence it cannot be included as a condition on the local query). There may be many other cases out there for remote queries.

We can also classify the queries in our application strictly as local & remote. If the DataSource is designed to always ignore local queries, then we get an application where we always use remote queries to fetch data from the server & load them into the store, while we always use local queries to get records matching particular conditions, out of the store. Personally, I like this approach very much as it imposes a certain order in the application.

This explanation of the difference between local and remote queries directly leads to another understanding - the need of conditions in local vs. remote queries.

Since local queries automatically try to evaluate if a record in the store matches the conditions of the query, conditions given on local queries need to be in a format that sproutcore can understand.

On the other hand, remote queries just use the list of storeKeys given by the DataSource to find the records that are part of its result. Hence the conditions given on a remote query do not matter. Ususally, remote queries are defined as constant properties in your application object & the DataSource just compares the incoming query object against one of these remote query objects and decides what needs to be fetched from the backend.

1 comment:

  1. Hi,
    I'm glad to see someone explain DataStore API.
    I think it is critical core peace in SproutCore but it has few documents about it.
    It will be great if you can write more about how to use DataStore, how to fetch records, and more important related records, what happen when I refresh a query when other records are related to the query been updated etc.

    Thank you,
    Ido

    ReplyDelete