Example #1
0
  public static long updateOldRecordsToServer() {
    long counter = 0;
    User user = BmobUser.getCurrentUser(CoCoinApplication.getAppContext(), User.class);
    if (user != null) {
      // already
      // login////////////////////////////////////////////////////////////////////////////////////
      isLastOne = false;
      for (int i = 0; i < RECORDS.size(); i++) {
        if (i == RECORDS.size() - 1) isLastOne = true;
        final CoCoinRecord coCoinRecord = RECORDS.get(i);
        if (!coCoinRecord.getIsUploaded()) {
          // has been
          // changed/////////////////////////////////////////////////////////////////////////////////
          if (coCoinRecord.getLocalObjectId() != null) {
            // there is an old coCoinRecord in server, we should update this
            // coCoinRecord///////////////////////////////////
            coCoinRecord.setUserId(user.getObjectId());
            coCoinRecord.update(
                CoCoinApplication.getAppContext(),
                coCoinRecord.getLocalObjectId(),
                new UpdateListener() {
                  @Override
                  public void onSuccess() {
                    if (BuildConfig.DEBUG) {
                      if (BuildConfig.DEBUG)
                        Log.d(
                            "CoCoin",
                            "recordManager.updateOldRecordsToServer update online "
                                + coCoinRecord.toString()
                                + " S");
                    }
                    coCoinRecord.setIsUploaded(true);
                    coCoinRecord.setLocalObjectId(coCoinRecord.getObjectId());
                    db.updateRecord(coCoinRecord);
                    // after updating, get the old records from
                    // server//////////////////////////////////////////////////
                    if (isLastOne) getRecordsFromServer();
                  }

                  @Override
                  public void onFailure(int code, String msg) {
                    if (BuildConfig.DEBUG) {
                      if (BuildConfig.DEBUG)
                        Log.d(
                            "CoCoin",
                            "recordManager.updateOldRecordsToServer update online "
                                + coCoinRecord.toString()
                                + " F");
                    }
                    if (BuildConfig.DEBUG) {
                      if (BuildConfig.DEBUG)
                        Log.d(
                            "CoCoin",
                            "recordManager.updateOldRecordsToServer update online code"
                                + code
                                + " msg "
                                + msg);
                    }
                  }
                });
          } else {
            counter++;
            coCoinRecord.setUserId(user.getObjectId());
            coCoinRecord.save(
                CoCoinApplication.getAppContext(),
                new SaveListener() {
                  @Override
                  public void onSuccess() {
                    if (BuildConfig.DEBUG) {
                      if (BuildConfig.DEBUG)
                        Log.d(
                            "CoCoin",
                            "recordManager.updateOldRecordsToServer save online "
                                + coCoinRecord.toString()
                                + " S");
                    }
                    coCoinRecord.setIsUploaded(true);
                    coCoinRecord.setLocalObjectId(coCoinRecord.getObjectId());
                    db.updateRecord(coCoinRecord);
                    // after updating, get the old records from
                    // server//////////////////////////////////////////////////
                    if (isLastOne) getRecordsFromServer();
                  }

                  @Override
                  public void onFailure(int code, String msg) {
                    if (BuildConfig.DEBUG) {
                      if (BuildConfig.DEBUG)
                        Log.d(
                            "CoCoin",
                            "recordManager.updateOldRecordsToServer save online "
                                + coCoinRecord.toString()
                                + " F");
                    }
                    if (BuildConfig.DEBUG) {
                      if (BuildConfig.DEBUG)
                        Log.d(
                            "CoCoin",
                            "recordManager.updateOldRecordsToServer save online code"
                                + code
                                + " msg "
                                + msg);
                    }
                  }
                });
          }
        }
      }
    } else {

    }

    if (BuildConfig.DEBUG) {
      if (BuildConfig.DEBUG)
        Log.d(
            "CoCoin",
            "recordManager.updateOldRecordsToServer update " + counter + " records to server.");
    }

    if (RECORDS.size() == 0) getRecordsFromServer();

    return counter;
  }