Search code examples
androidparse-platformback4app

ParseObject save locally. Error: Unable to encode an unsaved parse file


I need to save ParseObject with ParseFile, but locally. Method pinInBackground gives error: "Unable to encode an unsaved parse file"

       I can not call file.saveInBackground. Because I need to use offline mode.
       So what should I do?

        //get bitmap
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        mBitmap.compress(Bitmap.CompressFormat.JPEG, 50, baos);

        byte[] data = baos.toByteArray();
        Random random = new Random();

        //create parse File
        final ParseFile file = new ParseFile(random.nextInt(10000) + ".jpeg", data);

        parseObject.put(KEY_IMAGE, file);
        parseObject.pinInBackground(new SaveCallback() {
            @Override
            public void done(ParseException e) {
                //do some action
            }
        });

Solution

  • Let me create an answer for all of this.

    That error really does tell you what is wrong with the app. You can not pin something that isn't there. Parse pins objects.... you never created an object (yet). Therefore, you can not pin a unsaved object.

    You can do two things.

    1) If there is no internet connection, display the message to the user and tell them that. Once they restore, they can try again.

    2) Use saveEventually. This will save the object once internet hits the device, and then when done, you can pin it. Problem is, if the object doesn't exist and the user wants to see it, they cant.

    If it was me, I would go with option 1. If you are asking your user to input files, some sort of connection would be required.