Offline Synchronization using AIR and LiveCycle Data Services

As part of my MAX talks in Chicago and Barcelona, I used a simple Contact Manager application to demonstrate offline synchonization using AIR and LiveCycle Data Services (LCDS). Many people have asked me to share the code, so here it is… This is actually an update of an application I posted a few months ago (but that didn’t include the offline synchronization part at the time).
Using the Data Services to work with data in AIR offers several benefits:
First, just like for traditional browser-based Flex apps, the Data Management Service automates the synchronization of data between the client and the middle-tier. In other words, you don’t have to flag/keep track of the changes made at the client-side, and then make corresponding RPC calls to send changes to the server: this is all managed automatically.
Second, you can access and enter data while offline: the Data Management Service takes care of storing your data locally and synchronizing the changes with the server when you go back online. To enable offline synchronization, the Data Management Service API provides methods (such as saveCache() / clearCache()) to save data locally and manipulate the local data store. When you go back online, the commitRequired attribute of your dataService indicates if changes have been made offline. Invoking commit() on the dataService automatically synchronizes the offline changes with the server. See main.mxml for an example.
In the current AIR betas, the Flex Data Management Service uses Local Shared Objects as the local data store. For the AIR 1.0 release, we will use the SQLite database embedded in the AIR runtime instead.
In this application, I use the SQL assembler to provide the server-side persistence logic. Using the SQL assembler, you don’t have to write server-side components if you don’t want/need to: you just provide a series of SQL statements indicating how data should be retrieved, and how changes should be persisted in the database. The SQL assembler makes it extremely fast and easy to create applications that don’t require sophisticated persistence logic.
Installation instructions:
- Install LiveCycle Data Services 2.5 here
- In the samples web application, modify the flexdemodb database file to add the contact table: replace WEB-INF\db\flexdemodb\flexdemodb.script with this version.
- Open WEB-INF\flex\data-management-config.xml and add the following destination.
<destination id="sql-contact"> <adapter ref="java-dao" /> <properties> <use-transactions>true</use-transactions> <source>flex.data.assemblers.SQLAssembler</source> <scope>application</scope> <metadata> <identity property="CONTACT_ID"/> </metadata> <network> <session-timeout>20</session-timeout> <paging enabled="false" pageSize="10" /> <throttle-inbound policy="ERROR" max-frequency="500"/> <throttle-outbound policy="REPLACE" max-frequency="500"/> </network> <server> <database> <driver-class>org.hsqldb.jdbcDriver</driver-class> <url>jdbc:hsqldb:file:C:/lcds/jrun4/servers/default/samples/WEB-INF/db/flexdemodb/flexdemodb</url> <username>sa</username> <password></password> <login-timeout>15</login-timeout> </database> <actionscript-class>Contact</actionscript-class> <fill> <name>all</name> <sql>SELECT * FROM CONTACT</sql> </fill> <get-item> <sql>SELECT * FROM CONTACT WHERE CONTACT_ID = #CONTACT_ID#</sql> </get-item> <create-item> <sql>INSERT INTO CONTACT (FIRST_NAME,LAST_NAME,EMAIL,PHONE,ADDRESS,CITY,STATE,ZIP,COUNTRY,NOTES) VALUES (#FIRST_NAME#, #LAST_NAME#, #EMAIL#, #PHONE#, #ADDRESS#, #CITY#, #STATE#, #ZIP#, #COUNTRY#, #NOTES#)</sql> <id-query>CALL IDENTITY()</id-query> </create-item> <update-item> <sql>UPDATE CONTACT SET FIRST_NAME=#FIRST_NAME#,LAST_NAME=#LAST_NAME#,EMAIL=#EMAIL#,PHONE=#PHONE#,ADDRESS=#ADDRESS#,CITY=#CITY#,STATE=#STATE#,ZIP=#ZIP#,COUNTRY=#COUNTRY#,NOTES=#NOTES# WHERE CONTACT_ID=#_PREV.CONTACT_ID#</sql> </update-item> <delete-item> <sql>DELETE FROM CONTACT WHERE CONTACT_ID=#CONTACT_ID#</sql> </delete-item> <count> <name>all</name> <sql>SELECT count(*) FROM CONTACT</sql> </count> </server> </properties> </destination> - Start the server
- Install contacts.air and run the application
NOTE: The application has been compiled assuming that an RTMP endpoint named my-rtmp is configured at rtmp://localhost:2037. If your server isn’t configured that way, you will have to recompile the application. You can also provide the endpoints at runtime. See my previous post for more information on defining a ChannelSet.
You can download the source code here.
Application Walkthrough
- Start the application. Notice the green light in the upper right corner indicating that the dataService is connected to the server.
- Double-click a contact in the list to open the details form. Modify some data and click “Save”.
- Shut down your server. Notice that the connectivity status light is now red. Close the application to make it clear that your are not working with data cached in memory.
- Restart the application. It will take a second before you see the data: the dataService first tries to connect to the server before loading the data from the local cache. Open a contact, make some changes to the data and click “Save”. Your change is saved to the local cache.
- Close the application.
- Restart your server.
- Restart the application. Notice that the application automatically detects that changes have been made offline, and synchronizes the offline changes.
Comments
29 Responses to “Offline Synchronization using AIR and LiveCycle Data Services”
Leave a Reply









Christopher, thanks for this. Will your app work with the version of LiveCycle Data Services included in the ColdFusion 8 install?
[...] Christophe Coenraets released the code Code from his MAX session in Barcelona about Offline Synchronization using AIR and Live Cycle Data Services. [...]
@Mike,
Yes it will work you just need to configure the gateway and then of course build the services in CF
Just a couple of questions.
1. What if you have relational data?
Lets say I wanted to use your SalesBuilder Example and have it cache data locally and sync with lcds. Is it true that the cache doesn’t work with nested Destinations? (in it’s current form) lcds 2.5.1
2. In SalesBuilder would you have 3 Destinations and 3 dataservices each with their own cacheId. (Accounts, Contacts, Opportunities), if so would the destinations include relationships with each other)
By the way thanks for the examples, they have been extremely helpful.
[...] - Offline Synchronization using AIR and LiveCycle Data Services [...]
this is really great stuff! i am infinately impressed!
i am in the process of trying to speak to cf using the livecycle data services and am hoping to use your app as a platform for my education.
on this note, i have imported the source files to flex builder (v3 b2), but cannot compile due to some errors with types not being found and undefined properties.
i am fairly new to this all and am not too sure how to solve this.
any advice?
Hey Chris,
I’m at a bit of loss.
Can’t find WEB-INF\db\flexdemodb\flexdemodb.script
I’ve installed CF8 and then downloaded and installed the LCDS from adobe. Searched my HD for flexdemodb.
I must be missing something real obvious.
Thanks for you help
It is located at:
C:\Program Files\LCDS\jrun4\servers\default\samples\WEB-INF\db\flexdemodb
hello, the application is nice to get started. i have a question: is it possible to get a installed air application get connected to a destination on a extern server, not on the local computer???
I downloaded the source and tried to re-compile, but i am getting an error.
in main.mxml
private function conflictHandler(event:DataConflictEvent):void
is giving this error
1046: type was not found, or was not a compile time constant: DataConflictEvent
any clues?
Thanks
@todd: did you have an import of the DataConflictEvent?
is it possible to create an air app that doesn’t rely on local livecycle services in order to do this sort of thing… i.e. will all of our clients have to install LCDS when installing our app?
No idea were to ask this… hope anyone knows it…
Can i use the ‘Public’ java APIs in the LiveData Cycles for my application without paying the license of LiveData Cycles?
I what to use MessageBroker class.
THanks,
Dani.
Hi,
We are a young italian company working in IT. Since we choose to invest in adobe technology for all provided services (graphics, media, development web and desktop application) we are trying to offer our customers a set of new interesting feature in view of a market widening.
Actually we focus our attention on the possibility, that the LCDS allow, to store server database data locally and synchronize it when possible.
Your post ” Offline Synchronization using AIR and LiveCycle Data Services” was a very nice example, but the cached data of DataService object seems available only during the run time period of the air application, right?. The limit of 100 kilobytes is also another dramatic disadvantage.
Bear in mind that cached data should be always available on the client as first choose of our application and eventually should be possible synchronize it if server is available, embedded SQLite can help us?
How SQLite can synchronize it with server database?
I can use SQLite with DataService object?
Do you have any documentation or example about it?
I will be very glad if you can answer us and also If you have any alternative solution about this problem.
Kind regards
poker odds kalkyl…
incredulity sheepskin striving!library:corrosive acetone …
[...] Offline Synchronization using AIR and LiveCycle Data Services : Christophe Coenraets (tags: lcds air flex) [...]
Hi guy and thanks you so much for your example.
I’m searching on LCDS possibilities and I found your very tutorial.
I’ve got 2 problems :
- if I try to launch contact.air I’ve got this problem : “This application requires a version of Adobe AIR which is no longer supported. Please contact the application author for an updated version.”
- If I try to recompile but I got 2 errors :
“1046: type was not found, or was not a compile time constant: DataConflictEvent” in spite of the import “import mx.data.CacheDataDescriptor;” (like todd geist). And the other error is “1120 Acces to CacheDataDescriptor property not defined” at this line : “var token:AsyncToken = ds.getCacheData(dgCacheDescripors.selectedItem as CacheDataDescriptor);”
Somebody has an idea about solutions ?
Thank you for all.
illinois auto insurance…
boasting Korean!yelling …
uaojcd enporfv vkhup iuxvojm tsgeq lbrvjq cwpvrfnd
type was not found or not a compile time constant?? come onn
amateur porn dump
thanks ;)
thanks a lot ;)
teşekkürler ;)
thanks a lot ..
thanks
Thank you very much for this information. I like this site
The information is much useful. I like this article. Thanks for providing me the same.
thanks you very much.