Search code examples
androidparse-platformalarmmanagerreset

Android: Reset Parse backend object everyday, week and month


I am trying to reset parse int objects everyday, week and month using Alarm Manager. I have three objects dayObject, weekObject and monthObject which runs and updates the text views. But the app is crashing with error Value May Not be Null. Please help!!!

onCreate():

Intent iToday = new Intent(this, TodayReset.class);
iToday.putExtra("createdAt", createdAt.getTime());
piToday = PendingIntent.getBroadcast(this, 0, iToday, 0);
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, today.getTime(), AlarmManager.INTERVAL_DAY, piToday);

On Receive(): I am getting today, week and month dates from Calendar. Then I check the conditions and update the Parse backend.

ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Pattern");

query.whereEqualTo("username", ParseUser.getCurrentUser().getUsername());

query.orderByDescending("createdAt");

query.getFirstInBackground(new GetCallback<ParseObject>() {
   @Override
   public void done(ParseObject object, ParseException e) {
     if(e == null && object != null) {
       totalValue = object.getInt("totalValue");
       weekValue = object.getInt("weekValue");
       monthValue = object.getInt("monthValue");            
     } else {
       Log.i("RetErr", e.getMessage());
     }
   }
});

ParseObject pattern = new ParseObject("Pattern");

pattern.put("username", ParseUser.getCurrentUser().getUsername());

pattern.put("totalValue", totalValue);
pattern.put("todayValue", 0);
pattern.put("weekValue", weekValue);
pattern.put("monthValue", monthValue);

pattern.save();

I am repeating the same for week and month. I am retrieving all objects because when I save only a particular object, then the other objects show not defined. Hence I retrieve the old value and save it again.


Solution

  • Move the creation of the new Parse Object inside done() like below. Because the query is done in the background is doesnt finish before you try and use totalValue etc.

    ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Pattern");
    
            query.whereEqualTo("username", ParseUser.getCurrentUser().getUsername());
    
            query.orderByDescending("createdAt");
    
            query.getFirstInBackground(new GetCallback<ParseObject>() {
                @Override
                public void done(ParseObject object, ParseException e) {
    
                    if(e == null && object != null) {
    
                        totalValue = object.getInt("totalValue");
                        weekValue = object.getInt("weekValue");
                        monthValue = object.getInt("monthValue");
                        ParseObject pattern = new ParseObject("Pattern");
    
            pattern.put("username", ParseUser.getCurrentUser().getUsername());
    
            pattern.put("totalValue", totalValue);
            pattern.put("todayValue", 0);
            pattern.put("weekValue", weekValue);
            pattern.put("monthValue", monthValue);
    
                pattern.save();
    
    
                    } else {
    
                        Log.i("RetErr", e.getMessage());
    
                    }
    
                }
    
            });