Exception generated by "findDocumentByKeyValue"

0 votes

I want to develop a logic which finds the document using "findDocumentByKeyValue" and if such document not found then i want to create the document. For this use case i need to write Document creation code in "onException" of "findDocumentByKeyValue" method. Now problem is whnever coding flow enters into the  "onException" connection gets lost with error "Should stop return ......Missed 3 beats" something like this. 

Is there any solution for this ??

 

asked Jul 13, 2016 in App42 DevOps by testdemo772 (47 points)

1 Answer

0 votes
 
Best answer

Hi testDemo,

Greetings!!!!!!!!

Yes, you can create the Document in OnException callback method easily. but make sure it not be fall in loop. Please share your code so that I can validate why you are getting errors.

Let me know the details.

Thanks

Vishnu Garg

answered Jul 13, 2016 by Vishnu Garg (674 points)
selected Jul 13, 2016 by testdemo772
// My usecase is in doInBackground as below





        @Override
        protected Void doInBackground(Void... params) {

            App42API.initialize(context, Common.API_KEY, Common.PRIVATE_KEY);
            StorageService storageService = App42API.buildStorageService();

            System.out.println("EMAILLLLL===" + Common.profileEmail);

            storageService.findDocumentByKeyValue(dbName, userCollection, "Email", Common.profileEmail, new App42CallBack() {

                @Override
                public void onSuccess(Object o) {

                    Storage storage = (Storage) o;

                    ArrayList<Storage.JSONDocument> jsonDocList = storage.getJsonDocList();

                    for (int i = 0; i < jsonDocList.size(); i++) {


                        try {
                            Common.userJSONInfo = new JSONObject(jsonDocList.get(i).getJsonDoc());
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                        System.out.println("FOUND_DOCUMENT===" + Common.userJSONInfo);
                    }

                }

                @Override
                public void onException(Exception e) {

                    main_obj = new JSONObject();
                    try {

                        main_obj.put("Email", Common.profileEmail);
                        main_obj.put("Username", Common.userName);
                        main_obj.put("FBImageURL", Common.fbImageURL);
                        main_obj.put("BallotCollectTime", "");
                        main_obj.put("LoginType", Common.loginType);
                        main_obj.put("AccessToken", Common.accessToken);
                        main_obj.put("NumOfSpyGlasses", "0");
                        main_obj.put("NumOfCardBack", "0");
                        main_obj.put("CurrentCardBack", "");
                        main_obj.put("WeeklyBallot", "0");
                        main_obj.put("DailyBallot", "0");


                        JSONArray array = new JSONArray();
                        for (int h = 0; h < array.length(); h++) {
                            JSONObject jsonObject = new JSONObject();
                            jsonObject.put("CardBack", "");
                            array.put(jsonObject);

                        }
                        main_obj.put("CB_Array", array);

                        StorageService storageService = App42API.buildStorageService();
                        storageService.insertJSONDocument(dbName, userCollection, main_obj, new App42CallBack() {
                            public void onSuccess(Object response) {
                                Storage storage = (Storage) response;

                                ArrayList<Storage.JSONDocument> jsonDocList = storage.getJsonDocList();
                                for (int i = 0; i < jsonDocList.size(); i++) {
                                    try {
                                        Common.userJSONInfo = new JSONObject(jsonDocList.get(i).getJsonDoc());
                                    } catch (JSONException e) {
                                        e.printStackTrace();
                                    }

                                }
                            }

                            public void onException(Exception ex) {
                                Toast.makeText(context, "Something went wrong!! Try after sometime", Toast.LENGTH_SHORT).show();
                                System.out.println("Exception Message" + ex.getMessage());
                            }
                        });

                    } catch (JSONException e1) {
                        e1.printStackTrace();
                    }


                }
            });

            return null;
        }
As you are already using the async task, so I would like to suggest you to use sync api and do Exception handling else it will create thread inside other Thread
Can you suggest me some code snippets or references about Sync api to solve this problem
Storage storeData = storageService.insertJSONDocument(DBName,
                CollectionMeetings, jsonBody);
        System.out.println(storeData.getStrResponse());
        if (storeData.isResponseSuccess()) {
            ArrayList<Storage.JSONDocument> jsonDocList = storeData
                    .getJsonDocList();
            for (int i = 0; i < jsonDocList.size(); i++) {
                try {
                    response = new JSONObject(jsonDocList.get(i).getJsonDoc());
                   
            }
        }
Download Widgets
Welcome to ShepHertz Product line forum, where you can ask questions and receive answers from the community. You can also reach out to us on support@shephertz.com
...