Offline Synchronization using AIR and LiveCycle Data Services

contacts3.gif

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:

  1. Install LiveCycle Data Services 2.5 here
  2. 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.
  3. 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>
    			    <!-- Modify the URL below with the actual location of the flexdemodb database on your system -->
    			    <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> <!-- HSQLDB syntax to retrieve value of autoincremented column -->
    			</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>
    
  4. Start the server
  5. 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

  1. Start the application. Notice the green light in the upper right corner indicating that the dataService is connected to the server.
  2. Double-click a contact in the list to open the details form. Modify some data and click “Save”.
  3. 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.
  4. 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.
  5. Close the application.
  6. Restart your server.
  7. Restart the application. Notice that the application automatically detects that changes have been made offline, and synchronizes the offline changes.

, ,

  • Christopher, thanks for this. Will your app work with the version of LiveCycle Data Services included in the ColdFusion 8 install?

  • Pingback: Sönke Rohde » AIR examples/links, Desktop 2.0 and Adobe Developer Connection()

  • @Mike,

    Yes it will work you just need to configure the gateway and then of course build the services in CF

  • Chris Sharon

    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.

  • Pingback: Franto.com Flash blog » Presentations sources from Adobe Max 2007()

  • Nikki

    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

  • Elijah

    It is located at:

    C:\Program Files\LCDS\jrun4\servers\default\samples\WEB-INF\db\flexdemodb

  • robert

    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

  • robson

    @todd: did you have an import of the DataConflictEvent?

  • Nikki

    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?

  • Daniel F. Boada

    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.

  • Valerio Vitullo

    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

  • Pingback: poker odds kalkyl()

  • Pingback: 17 Pens » links for 2008-04-10()

  • trotters213

    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.

  • Pingback: illinois auto insurance()

  • uaojcd enporfv vkhup iuxvojm tsgeq lbrvjq cwpvrfnd

  • type was not found or not a compile time constant?? come onn

  • 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.

  • Pingback: About LCDS « It’s all about RIA()

  • Pingback: Adobe LCDS « Flex Generation Weblog()

  • Ebby

    Hello everyone. Please i have a problem. I did a sample application that pushes content realtime, with flex and livecycle on coldfusion. However, when i try making it an Air application. it doesn’t work. I get “consumer error”.

    Pls how do i solve this problem? is there a special way to use air with livecycle and coldfusion..

    thanks

  • Pingback: Adobe LCDS « SrikanthCreative Mind’s Weblog()

  • Pingback: on the internet cheapest calorie meter clock()

  • Hello everyone. Please i have a problem. I did a sample application that pushes content realtime, with flex and livecycle on coldfusion. However, when i try making it an Air application. it doesn’t work. I get “consumer error”.

    Pls how do i solve this problem? is there a special way to use air with livecycle and coldfusion..

  • 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.

  • thank you

  • thank you

  • 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 must be missing something real obvious.

  • Thank you very much

  • type was not found or not a compile time constant?? come onn

  • tanky

  • thaks you

  • Thanks My Friend..

  • thank you

  • sohbet

    hallo i wish you verry succes operator

  • Thank You

  • Thank you.

  • thankss yourss verfy good life

  • chat thanks

  • sohbet thankss

  • 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.

  • 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.

  • ssk

    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

  • The information is much useful. I like this article. Thanks for providing me the same

  • 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,

  • But not very cool

  • thanx admins

  • Thank you for post.

  • thankss yourss verfy good life

  • But not very cool

  • thank you mennnnnnnnnnÇ:S

  • But not very cool

  • thanks good services

  • So if both are good, together they

  • thank you so much nice post

  • admin thanks you for sharing sende olmasan napacaz biz . :)

  • nice sharing. man thank you :)
    çelik kapı

  • Good info, numerous thanks to the author. It’s puzzling to me now, generally, the usefulness as well as significance is overpowering. Greatly thanks once more and best of luck!

  • Great post. I was checking constantly this blog and I’m impressed! Extremely useful info specifically the last part :) I care for such information a lot. I was seeking this certain information for a long time. Thank you and good luck.

  • really liked your presentation on QCON a few weeks ago. I could really tell you are very good at what you do. My compliment for that.

    I have a question though: I some environments like banks etc Flash plugin is disabled in the browser (security wise). How can we still use this technology wit

  • I think one of your adverts caused my window to resize, you may well want to put that on your blacklist.

  • Prometeus

    Hi Christophe! Can you help? I can’t download the source code, when I go after the link it says: Not Found | The requested URL /downloads/contacts/contacts-src.zip was not found on this server.
    Thanks.

  • Pingback: life cycle data service and mx.data.* classes | msn messenger smiley()

  • Pingback: What are some good examples or paradigms for Client and Server implementation in the same source file? | Stack One()

  • Thanks really been a very successful sites

  • thnnx text

  • obrg gracias amigos!

  • You can visit our store for all our other models. Please contact us for you to design your own jersey. Please contact us for further information.

  • great job

  • everything up to date information here Fun music video story!

    çelik raf

css.php