Sahil Dave
Sahil Dave

Reputation: 433

How to delete all objects older than NOW?

I am working on an Android App and have a class where user can update his "choices". This is from a Dialog Box which appears when a user installs the app, BUT he can also access it later on via overflow. At the startup, according to the choices by user, I upload a class, NCUP.class on Parse.com. But when user clicks on overflow icon to change preferences, I want to "refresh" the table by deleting ALL the previous records and adding new ones. I was previously using SQLite database and I just used

database.delete(MySQLiteHelper.TABLE_NCUP, null, null);

Now, I can think 3 ways:

  1. (Currently doing) As soon as the user clicks "Done" after selecting his pref, I delete all the objects in the table by below code, but this is taking upto 5 seconds for a class with 13 entries.:

        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    Date date = new Date();
    String current = dateFormat.format(date);
    
    ParseQuery<NCUP> query = ParseQuery.getQuery(NCUP.class);
    query.equals("objectId");
    
    // query.whereLessThan("createdAt", date);
    
    query.findInBackground(new FindCallback<NCUP>() {
    
        @Override
        public void done(List<NCUP> ncupList, ParseException e) {
            if (e == null) {
                Log.d("QUERY", "Retrieved " + ncupList.size() + " data");
                Log.d("QUERY", ncupList.get(1).getCreatedAt().toString());
                for (NCUP ncup : ncupList) {
                    try {
                        ncup.delete();
                    } catch (ParseException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                }
    
            } else {
                Log.d("QUERY", "Error: " + e.getMessage());
            }
        }
    
    });
    
  2. Get the query by using the line that I have commented above i.e by using current date and adding to query whereLessThan to get and delete older items. But this doesnt work, returns ncupList.size() as 0. Tried different formatting but none helped.

  3. Drop the whole NCUP class and make a new one. I tried to search for this method for it seems to me fastest, but I think I can not do it programatically.

Any suggestions?

Upvotes: 1

Views: 413

Answers (1)

Peter
Peter

Reputation: 540

The problem might be that you are using ncup.delete(), which is executing an http request on the main thread.

Replace with ncup.deleteInBackground() and your UI won't block.

Also look into ParseObject.deleteAllInBackground(List<ParseObject> objects, DeleteCallback callback) which would seem to do what you need (deleting a list of ParseObjects)

Upvotes: 1

Related Questions