Topic
IC4NOTICE: developerWorks Community will be offline May 29-30, 2015 while we upgrade to the latest version of IBM Connections. For more information, read our upgrade FAQ.
5 replies Latest Post - ‏2013-01-23T15:39:36Z by rtimothy
SystemAdmin
SystemAdmin
2327 Posts
ACCEPTED ANSWER

Pinned topic Using WL.JSONStore to synchronize across multiple devices

‏2013-01-04T15:24:52Z |
Hello,

I have a use case where I have a server-side database and multiple clients, where each would hold a copy of the database records (or a subset of them). On any of these devices I need to be able to add new documents to its local JSONStore, push those changes to the server, and then have the other devices pull the updates and refresh their local JSONStore. So it's a common push/pull sync scenario across multiple devices.

I have played with the JSONStore and my app can push documents entered on one device (A) and store in the database on the server side. That works just great.

But when I launch the app on a second device (B) and try to pull the database records none are loaded into B's JSONStore. If I create new entries on B I can push them to the server and they appear in the database. But A can not pull them into its JSONStore.

Does the JSONStore implement some kind of device id or filtering that causes my devices not to see each others database entries?
Is this a use case that is supposed to work work with the JSONStore, or was it not designed for cross-device updates?

I'll try to write a smaller test case and see if I can troubleshoot it further.

Thanks,
Henrik Sjöstrand
  • rtimothy
    rtimothy
    22 Posts
    ACCEPTED ANSWER

    Re: Using WL.JSONStore to synchronize across multiple devices

    ‏2013-01-04T16:11:08Z  in response to SystemAdmin
    You are correct in that there isn't an "auto sync" feature in JSONStore, however there are mechanisms to implement exactly what you described. First off, how do you load data into the JSONStore? Are you using the load: true flag to initCollection? If so, there's a check in there that will not load data if there is already items in the store. You can do a couple of things to get the latest copy of the data, 1) drop all the records in the collection and simply reload with the latest data (fine if your dataset is small). 2) use the refresh command to replace items in the collection. For example, call you service and retrieve the updated records, then use JSONStore replace to update those records in JSONStore, replace has the semantics of not marking the record as needing push implying that it is a "fresh" copy from the server.
    --tim
    • SystemAdmin
      SystemAdmin
      2327 Posts
      ACCEPTED ANSWER

      Re: Using WL.JSONStore to synchronize across multiple devices

      ‏2013-01-04T17:51:47Z  in response to rtimothy
      Thanks for the reply. I am indeed using load:true so perhaps that's the catch. I'm also using an SQL adapter sitting on the serverside.

      For option 1) to drop all records I assume the removeCollection API is the easiest, and then to do a new initCollection with the load:true flag?

      Is there any mechanism in JSON Store which can automatically refresh the local store with data that has been updated on the server side, or does one have to keep track of updated objects serverside and call refresh on each individual document in the JSON store?

      /Henrik Sjöstrand
      • rtimothy
        rtimothy
        22 Posts
        ACCEPTED ANSWER

        Re: Using WL.JSONStore to synchronize across multiple devices

        ‏2013-01-04T19:32:03Z  in response to SystemAdmin
        > HenrikSjostrand wrote:
        > Thanks for the reply. I am indeed using load:true so perhaps that's the catch. I'm also using an SQL adapter sitting on the serverside.
        >
        > For option 1) to drop all records I assume the removeCollection API is the easiest, and then to do a new initCollection with the load:true flag?

        Yes.

        >
        > Is there any mechanism in JSON Store which can automatically refresh the local store with data that has been updated on the server side, or does one have to keep track of updated objects serverside and call refresh on each individual document in the JSON store?
        >

        Not currently.

        > /Henrik Sjöstrand
        For option 1) to drop all records I assume the removeCollection API is the easiest, and then to do a new initCollection with the load:true flag?
        • SystemAdmin
          SystemAdmin
          2327 Posts
          ACCEPTED ANSWER

          Re: Using WL.JSONStore to synchronize across multiple devices

          ‏2013-01-23T07:19:27Z  in response to rtimothy
          rtimothy
          If we remove the local JsonStore and reload it again to get the fresh data from backend then
          we have to compromise our local changes on JsonStore which we wanted to be reflected to the
          backend along with the fresh data set. What do you suggest to handle this situation?

          What I am trying to say is suppose we retrieved a data set from backend and saved it in JsonStore,
          now I am updatind/adding some record to the JsonStore and like to push this to the backend system
          but at the same time some one updated the backend data system. So when I try to push my JsonStore records
          to the backend at will get a conflict , if I try to get the fresh record then I have to drop my changes.
          How this conflict of data transaction can be solved while using JsonStore? Can we set any priority like
          when device is trying to update data to backend then immediate data transaction on backend data system will be
          rolled back ?
          • rtimothy
            rtimothy
            22 Posts
            ACCEPTED ANSWER

            Re: Using WL.JSONStore to synchronize across multiple devices

            ‏2013-01-23T15:39:36Z  in response to SystemAdmin
            The way to handle this situation would be to create a procedure on the client to handle the resolution of changes. Take the following scenario for example:
            1) Client loads data from the server into JSONStore (everything is in sync)
            2) Client updates data in the JSONStore (client is now out of sync with server)
            3) Something changes on the server (client and server both have changes)
            4) On the client, grab the data from the server using the WL.invokeProcedure API (this is what JSONStore does under the covers) to get the new data from the server.
            5) For each record you get back from the server, check if the record has been modified in JSONStore ( isPushRequired() ), if it has not been modified locally use the refresh() method to update the local copy. If it has been modified locally, you will need to figure out how to resolve the conflict ( present a UI to the user, take the server one, take the local one etc) and then use either the replace() or refresh() call on the client to update the JSONStore accordingly.