Topic
6 replies Latest Post - ‏2013-01-28T15:43:33Z by SystemAdmin
djna
djna
7 Posts
ACCEPTED ANSWER

Pinned topic JSONStore.removeCollection() - error code 26?

‏2013-01-25T07:49:40Z |
I am calling

var status = myCollection.removeCollection(options);

The return value, status is undefined.

Defining callbacks in my options parameter I see the error callback fired, receiving an error code of 26.

This is documented as 26 = "CLEARING_COLLECTION";

Any suggestions as to the problem? All other operations on the collection seem to work just fine, both before and after this removeCollection() call, which is clearly having no effect - hence I know that the collection is correctly initialised.
  • SystemAdmin
    SystemAdmin
    2327 Posts
    ACCEPTED ANSWER

    Re: JSONStore.removeCollection() - error code 26?

    ‏2013-01-25T12:51:29Z  in response to djna
    You did not provide enough information for me to help you.

    Where are you running your code (Android Emulator 2.2, iPhone 4S iOS 6, ...)?

    It would also be helpful if you post the code you're using to work with JSONStore or a simplified version that illustrates the problem.

    Try the following:
    • Create a new collection (expect status 0)
    • Store 1 document.
    • Count the documents inside the collection (expect 1)
    • Remove the collection
    • Re-create the collection (expect status 0)
    • Re-count the documents inside the collection (expect 0)

    Here's the code:

    
    var c = WL.JSONStore.initCollection(
    'c', 
    {fn: 
    'string'
    }, 
    {onSuccess: function (status) 
    {   WL.Logger.debug(
    'Status of the collection is: ' + status);   c.store(
    {fn: 
    'carlos'
    }, 
    {onSuccess: function (count) 
    {   WL.Logger.debug(
    'Stored: ' + count + 
    ' document(s).');   c.count(
    {onSuccess: function (total) 
    {   WL.Logger.debug(
    'Total documents stored: ' + total);   c.removeCollection(
    {onSuccess: function (status) 
    {   WL.Logger.debug(
    'Remove collection finished, status: ' + status);   c = WL.JSONStore.initCollection(
    'c', 
    {fn: 
    'string'
    }, 
    {onSuccess: function () 
    {   WL.Logger.debug(
    'Status of the collection is: ' + status);   c.count(
    {onSuccess: function (total) 
    {   WL.Logger.debug(
    'Total documents stored: ' + total);   
    }
    });
    //end count 2   
    }
    });
    //end init 2   
    }
    });
    //end removeCollection   
    }
    });
    //end count   
    }
    }); 
    //end store   
    }
    });
    //end init
    


    Here's the expected output on iOS:

    
    [LOG] Status of the collection is: 0 [LOG] Stored: 1 document(s). [LOG] Total documents stored: 1 [LOG] Remove collection finished, status: 0 [LOG] Status of the collection is: 0 [LOG] Total documents stored: 0
    


    Here's the expected output on Android:

    
    D/wlapp (278): Status of the collection is: 0 D/wlapp (278): Stored: 1 document(s). D/wlapp (278): Total documents stored: 1 D/wlapp (278): Remove collection finished, status: 0 D/wlapp (278): Status of the collection is: 0 D/wlapp (278): Total documents stored: 0
    


    Note: Make sure you call all your JSONStore code after wlCommonInit is called.

    As a side-note, most of the API is a synchronous. That means you won't get return values doing something like this:

    
    var status = myCollection.removeCollection(options);
    


    You get return codes as the first parameter of the onSuccess callback and error codes as the first parameter of the onFailure callbacks.
    • SystemAdmin
      SystemAdmin
      2327 Posts
      ACCEPTED ANSWER

      Re: JSONStore.removeCollection() - error code 26?

      ‏2013-01-25T12:57:19Z  in response to SystemAdmin
      *most of the API is asynchronous
      • djna
        djna
        7 Posts
        ACCEPTED ANSWER

        Re: JSONStore.removeCollection() - error code 26?

        ‏2013-01-25T14:07:31Z  in response to SystemAdmin
        Thank you for your response, I have noted that the API asynchronous.

        I've run your suggested code and get the same error as in mine. I needed to add an error callback for removeCollection() as shown below. I get the same output as you unti I get to remove collection, then I hit the onFailure method with a status code of 26.

        I am developing on Windows using Worklight Studio, 5.0.5.20121129-102, I'm testing in the Android Emulator using a SDK level of 14.

        c.removeCollection({onSuccess: function (status) {

        WL.Logger.debug('Remove collection finished, status: ' + status);

        // etc

        }, onFailure: function (status){
        WL.Logger.debug('Remove collection failed, status: ' + status);
        }
        • SystemAdmin
          SystemAdmin
          2327 Posts
          ACCEPTED ANSWER

          Re: JSONStore.removeCollection() - error code 26?

          ‏2013-01-25T16:09:57Z  in response to djna
          I was able to reproduce the issue using the same version of the Android emulator you're using (SDK 14).

          For now a work around is to use another version of the SDK. Try with Android 2.2 API Level 8.
          • djna
            djna
            7 Posts
            ACCEPTED ANSWER

            Re: JSONStore.removeCollection() - error code 26?

            ‏2013-01-25T16:30:56Z  in response to SystemAdmin
            Thanks it's good to know that the problem is reproducible. For some reason my Dojo code will not initialise in Android v 8, so I'm stuck on v14 for the moment. It's not an urgent problem, but I'd appreciate any updates if you can see a resolution.
            • SystemAdmin
              SystemAdmin
              2327 Posts
              ACCEPTED ANSWER

              Re: JSONStore.removeCollection() - error code 26?

              ‏2013-01-28T15:43:33Z  in response to djna
              It's resolved in our dev build.

              For now, maybe you can try to the following:
              • Try to call removeCollection
              • capture the error callback
              • do a findAll to get all the documents inside the collection
              • call remove() once per document.

              This is untested and depends on a third party library, but maybe it gets the idea across by complementing the steps above:

              Add to your main HTML file:
              
              <script src=
              "https://raw.github.com/caolan/async/master/lib/async.js"></script>
              


              Call enhance after you init the collection:
              
              var c = 
              
              null; 
              //Some editors complains about c not being initialized   var enhanceRemoveCollection = function () 
              {   c.enhance(
              'removeCollectionFix', function (options) 
              {   var collection = 
              
              this; 
              //keep context   var manuallyRemoveDocuments = function (ctx) 
              {   WL.Logger.debug(
              'Manually removing all the elements');   
              //Find all the documents in the collection and remove the 1 by 1 collection.findAll(
              {onFailure: options.onFailure, onSuccess: function (docs) 
              {   WL.Logger.debug([
              'Found:', docs.length,
              'document(s).'+ 
              'Documents to JSON:', JSON.stringify(docs)].join(
              ' '));   var arr = [], len = docs.length, iter = len;   
              
              while (len--) 
              { arr.push(function (cb) 
              {   iter--;   collection.remove(docs[iter], 
              {onSuccess: function () 
              {   WL.Logger.debug(
              'Removed a document.'); cb();   
              }, onFailure: function (err) 
              {   WL.Logger.debug([
              'Failed to remove a document, returning with:', err, 
              'msg:',WL.JSONStore.getErrorMessage(err)].join(
              ' '));   options.onFailure(err);   
              }
              });
              //end remove   
              });
              //end array push   
              }
              //end while to populate arr   
              //See: https://github.com/caolan/async#series async.series(arr, function () 
              { WL.Logger.debug(
              'Finished removing everything.'); options.onSuccess(0);
              //status code 0 == success 
              });   
              }
              });
              //end findAll   
              };
              //end manuallyRemoveDocuments   
              //First try to remove using the normal call, if that fails use the enhanced version collection.removeCollection(
              {onSuccess: options.onSuccess, onFailure: manuallyRemoveDocuments
              });   
              });
              //end enhance call on collection c   
              }; 
              //end enhanceRemoveCollection   c = WL.JSONStore.initCollection(
              'c', 
              {fn: 
              'string'
              }, 
              {onSuccess: enhanceRemoveCollection
              });
              


              You would use it like this:

              
              var options = 
              {
              }; options.onSuccess = function () 
              { WL.Logger.debug(
              'Success'); 
              }; options.onFailure = function () 
              { WL.Logger.debug(
              'Failure'); 
              }; c.removeCollectionFix(options);
              


              If that doesn't work there's no much else you can do, except maybe trying to run your code on other versions of the Android SDK (not necessarily API Level 8, that was just an example I tested where it works fine).