Android

Problem with ParseFile in offline mode

Ashok

14 Dec 2014

Problem with ParseFile in offline mode

Parse for its obvious reasons has been our go to MBaaS. We have been using Parse extensively for all our server needs which otherwise would require us to define our own REST services and incur additional time delay and cost to our clients.

In one of our applications, we were using ParseFile and ParseObject to exchange data with Parse.com. Things worked out well in online mode but when we decided to support app also in offline mode, we had a surprise in store!

Dealing with ParseFile in offline mode

Dealing with ParseObject in offline mode was quite straight forward but ParseFile posed a big problem!!. When we try to save a ParseFile in offline mode, we were getting exception java.lang.IllegalStateException: Unable to encode an unsaved ParseFile.While ParseObjects worked out just fine with SaveEventually calls.

We spent two days trying to solve this issue with ParseFile and then we finally decided to mock ParseFile, by storing the byte array directly as part of ParseObject. As any ParseObject can take a byte array, It was that easy for us to manage files as a byte array and we were able to smoothly integrate this solution in the app.

We are looking at formidable support for ParseFile from Parse.com in offline mode still. Till that comes out, I think our approach of storing the byte array in ParseObject helped us manage the offline functionality of the app. Sometimes, doing this looked easier than storing ParseFile first and then linking it back to the object in its successful save callback. As we were not doing much size reduction or content filtering within the files, our approach worked out pretty well for us.

Instead of the usual steps below

1. Get the byte array of file

2. Put the bytes array into a ParseFile

3. Call ParseFile#saveInBackground() and wait for its success saveCallback

4. Put the ParseFile object in ParseObject

5. Call ParseObject#saveEventually()

We followed the below steps:

1. Get the byte array of file

2. Put the byte array into ParseObject

3. Call ParseObject#saveEventually()