Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android, save files in offline mode (parse.com)

parse.com provide a way to save object in offline mode with object.saveEventually(); call.. this stores object in local dataStore unless the syncing done with server.

the problem is,I can not store file with below given lines

ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 80, stream);
byte[] byteArray = stream.toByteArray();
file = new ParseFile("pic.png",byteArray);  
object.put("file.png", file);   
object.saveEventually();

it gives following error

java.lang.IllegalStateException: Unable to encode an unsaved ParseFile

according to parse documentation to save file file.saveInBackground should be called... and I think saveInBackground only works when there is internet connection...

but the thing which I want to implement is get data (object or file) from local dataStore until sync is done with server...

so how can I save file in such a way in offline mode that I can access that file from local dataStore until the sync is done with the server...

like image 538
Naveed Ali Avatar asked May 16 '14 12:05

Naveed Ali


3 Answers

I also faced the same problem, I did like this to match my requirements. I hope it might be helpful.

How to pin a ParseObject with a ParseFile to the Parse Local Datastore in OFFLINE?

like image 181
ashokgujju Avatar answered Sep 27 '22 20:09

ashokgujju


save your object eventually and in the callback of saveeventually call saveinbackground of parsefile and again put that file in your object and save it eventually. This will save your file offline

like image 22
vaibhav Avatar answered Sep 27 '22 20:09

vaibhav


You never call call save on the file (read the error carefully: "Unable to encode an UNSAVED ParseFile")

You must first save the file - when that completes you can then call save on the object that references the file.

I recommend you call file.saveInBackground() using callbacks. https://parse.com/docs/android/api/com/parse/ParseFile.html#saveInBackground(com.parse.SaveCallback, com.parse.ProgressCallback)

example:

private void saveImageOnParseWithCallbacks(){

parseFile = new ParseFile(gate_image.getName(), byte_image);
parseFile.saveInBackground(new SaveCallback() {
    @Override
public void done(ParseException e) {                          
                if (e == null) {
                    saveParseObject(); // this is where you call object.SaveEven....
                }else {
                   e.printStackTrace(System.err);
                }
              }   
             }, new ProgressCallback() {
               @Override
           public void done(Integer percentDone) {
              // Update your progress spinner here. percentDone will be between 0 and 100.
                }
              });
    }

Look at parse's new local data store. It works for ParseObjects but not ParseFiles (as far as i know) For an offline solution you will have to pin object files first and store the image locally on sdcard. When the savecallback on object completes you can save the image (i.e. you know you have internet restored). When the image savecallback completes you can update the object with a ref to the image.

https://parse.com/docs/android/api/com/parse/ParseObject.html#pinAllInBackground(java.lang.String, java.util.List, com.parse.SaveCallback)

like image 27
navraj Avatar answered Sep 27 '22 20:09

navraj