@Override
  protected void onHandleIntent(Intent intent) {
    Log.e(TAG, "gettting donation requests");

    TbsService service = ServiceManager.getInstance();
    try {
      Call<List<DonateApproval>> call = service.getDonationRequests();
      Response<List<DonateApproval>> response = call.execute();

      if (response.code() == HttpURLConnection.HTTP_OK) {
        List<DonateApproval> donateApprovals = response.body();

        Realm realm = Realm.getDefaultInstance();
        realm.beginTransaction();
        realm.where(DonateApproval.class).findAll().clear();
        realm.copyToRealmOrUpdate(donateApprovals);
        realm.commitTransaction();
        realm.close();

        notifySuccess(ACTION, "Successful");
      } else {
        String error = response.errorBody().string();
        Log.e(TAG, "Error: " + error);
        notifyFailure(ACTION, "Failed");
      }

    } catch (Exception e) {
      Log.e(TAG, e.getMessage(), e);
      notifyFailure(ACTION, "Request error");
    }
  }
  @Override
  protected void doOneByOneCrudRun(int count) throws Exception {
    final List<SimpleEntityNotNull> list = new ArrayList<>();
    for (int i = 0; i < count; i++) {
      list.add(SimpleEntityNotNullHelper.createEntity((long) i));
    }

    startClock();
    for (int i = 0; i < count; i++) {
      realm.beginTransaction();
      realm.copyToRealm(list.get(i));
      realm.commitTransaction();
    }
    stopClock(Benchmark.Type.ONE_BY_ONE_CREATE);

    startClock();
    for (int i = 0; i < count; i++) {
      realm.beginTransaction();
      realm.copyToRealmOrUpdate(list.get(i));
      realm.commitTransaction();
    }
    stopClock(Benchmark.Type.ONE_BY_ONE_UPDATE);

    deleteAll();
  }
  @Override
  protected void onHandleIntent(Intent intent) {
    Log.e(TAG, "getting notifications. . . ");
    String username = intent.getStringExtra(Constants.User.USERNAME);
    TBSUserInterface service = ServiceManager.getInstance();
    try {
      Call<List<Notification>> call = service.getNotifications(username);
      Response<List<Notification>> response = call.execute();

      if (response.code() == HttpURLConnection.HTTP_OK) {
        List<Notification> notifications = response.body();
        Log.e(TAG, response.body().toString());

        Realm realm = Realm.getDefaultInstance();
        realm.beginTransaction();
        realm.where(Notification.class).findAll().clear();
        realm.copyToRealmOrUpdate(notifications);
        realm.commitTransaction();
        realm.close();

        notifySuccess(ACTION, "Successful");
      } else {
        String error = response.errorBody().string();
        Log.e(TAG, "Error: " + error);
        notifyFailure(ACTION, "Error");
      }

    } catch (IOException e) {
      Log.e(TAG, e.getMessage(), e);
      notifyFailure(ACTION, "Unable to connect to server");
    }
  }
  @Subscribe
  public void onMovieSearchSuccess(TitleSearchSuccessEvent event) {
    MovieFull movieFull = event.getMovieFull();
    if (!movieFull.isResponse()) {
      // failed set error on input layout
      movieSearchInputLayout.setError(movieFull.getErrorMessage());
    } else {
      // actual success
      realm.beginTransaction();
      realm.copyToRealmOrUpdate(
          new PreviousSearch(
              movieFull.getImdbId(),
              movieFull.getTitle(),
              movieFull.getPoster(),
              new Date(),
              movieFull.getYear()));
      realm.commitTransaction();

      // doing it this way to maintain descending order easily
      getListFromRealm();

      recentSearchesRecycler.getAdapter().notifyDataSetChanged();

      // clear text
      movieSearch.setText("");

      // 2 move to detail screen
      Intent i = new Intent(MainActivity.this, MovieDetailActivity.class);
      i.putExtra(MovieDetailActivity.ARG_MOVIE, event.getMovieFull());
      startActivity(i);
    }
  }
示例#5
0
 public void saveToDatabase() {
   if (mEntityList.size() > 0)
     for (int i = mEntityList.size() - 1; i >= 0; i--) {
       mRealm.beginTransaction();
       RealmZcool realmZcool = new RealmZcool();
       realmZcool.setFromEntity(mEntityList.get(i));
       mRealm.copyToRealmOrUpdate(realmZcool);
       mRealm.commitTransaction();
     }
 }
  public void updateFinished(OpenweathermapObject data) {
    String errorMessage = getString(R.string.txt_unknown_error);

    if (data != null) {
      errorMessage = data.getErrorMessage();
    }

    if (errorMessage.isEmpty()) {
      SharedPreferences preferences =
          PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
      Boolean del_old =
          preferences.getBoolean(
              getString(R.string.settings_delete_old_data_key),
              getResources().getBoolean(R.bool.settings_delete_old_data_default));
      Realm realm = null;
      try {
        realm = Realm.getDefaultInstance();
        realm.beginTransaction();
        if (del_old) {
          Long timeStamp = data.getWeatherForecastList().get(0).getTimeStamp();
          RealmResults<ForecastListItem> result =
              realm.where(ForecastListItem.class).lessThan("timeStamp", timeStamp).findAll();
          result.clear();
        }
        realm.copyToRealmOrUpdate(data.getWeatherForecastList());
        realm.commitTransaction();

        SharedPreferences.Editor editor = preferences.edit();
        editor.putLong(getString(R.string.last_update_key), System.currentTimeMillis());
        String city = data.getCityName().concat(", ".concat(data.getCountry()));
        editor.putString(getString(R.string.current_city_key), city);
        editor.commit();

      } finally {
        if (realm != null) {
          realm.close();
        }
      }
    }
    Intent intent = new Intent(BROADCAST_ACTION);
    intent.putExtra(PARAM_STATUS, STATUS_FINISH);
    intent.putExtra(PARAM_RESULT, errorMessage);
    sendBroadcast(intent);
  }
  public void loadToRealm() throws JSONException, ParseException {
    int count = JSONlist.length();

    Realm realm = Realm.getDefaultInstance();

    realm.beginTransaction();
    realm.clear(MyRealmItem.class);
    for (int i = 0; i < count; i++) {
      MyWeatherItem myWeatherItem = new MyWeatherItem((JSONObject) JSONlist.get(i));
      realm.copyToRealmOrUpdate(myWeatherItem.realmItem);
    }
    realm.commitTransaction();
    RealmQuery<MyRealmItem> query = realm.where(MyRealmItem.class);
    results = query.findAll();
    if (adapter != null) {
      adapter.notifyDataSetChanged();
    }
    //        realm.close();
  }
 // Todoデータを登録
 public boolean insertOrUpdateTodo(Todo todo) {
   synchronized (this) {
     if (todo.getId() == 0) {
       // 登録されているTodoデータの最大idを取得し、+1 したものをidとする(つまり連番)
       todo.setId(selectTodoMaxId() + 1);
     }
     // トランザクション開始
     realm.beginTransaction();
     try {
       // idにプライマリキーを張ってあるため既に同一idのデータが存在していれば更新となる
       realm.copyToRealmOrUpdate(todo);
       // コミット
       realm.commitTransaction();
     } catch (Exception e) {
       // ロールバック
       realm.cancelTransaction();
       return false;
     }
     return true;
   }
 }
  public void callAddUpdateDiscipline(View view) {
    String label = "atualizada";
    if (discipline.getId() == 0) {
      disciplines.sort("id", RealmResults.SORT_ORDER_DESCENDING);
      long id = disciplines.size() == 0 ? 1 : disciplines.get(0).getId() + 1;
      discipline.setId(id);
      label = "adicionada";
    }

    try {
      realm.beginTransaction();
      discipline.setName(etName.getText().toString());
      realm.copyToRealmOrUpdate(discipline);
      realm.commitTransaction();

      Toast.makeText(AddUpdateDisciplineActivity.this, "Disciplina " + label, Toast.LENGTH_SHORT)
          .show();
      finish();
    } catch (Exception e) {
      e.printStackTrace();
      Toast.makeText(AddUpdateDisciplineActivity.this, "Falhou!", Toast.LENGTH_SHORT).show();
    }
  }
 @Override
 protected Void doInTask(String... params) {
   String priceString = params[0];
   String myCode = LxbgApp.getInstance().LoginCode;
   fetchingJson(
       com.hlk.lxbg.customer.api.Order.OrderApi(),
       com.hlk.lxbg.customer.api.Order.UpPrice(myCode, orderId, priceString, orderUser));
   if (succeed && null != result && result.getS() == 0) {
     // 更改价格成功
     Realm realm = RealmConfig.getInstance().getRealm();
     try {
       Order order = new OrderImpl(realm).selectById(orderId);
       // 更新我对这个订单的报价状态
       realm.beginTransaction();
       order.setPrice(priceString);
       order.setPayState("1");
       realm.copyToRealmOrUpdate(order);
       realm.commitTransaction();
     } finally {
       realm.close();
     }
   }
   return null;
 }
示例#11
0
 private void insertFakeToRealm() {
   V realmObj = getFakeRealm();
   realm.beginTransaction();
   realm.copyToRealmOrUpdate(realmObj);
   realm.commitTransaction();
 }
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_realm_basic_example);
    rootLayout = ((LinearLayout) findViewById(R.id.container));
    rootLayout.removeAllViews();

    Realm.deleteRealm(new RealmConfiguration.Builder(this).build());
    realm = Realm.getInstance(this);

    //        {
    //            long timeMillis = System.currentTimeMillis();
    //            for (int i = 0; i < 10000; i++) {
    //                BasicContact contact = new BasicContact();
    //                contact.address = "john" + i + "@example.com";
    //                contact.number = "#" + i;
    //                contact.active = true;
    //            }
    //            Log.e("IntroExampleActivity", "Create Object Time#: " +
    // (System.currentTimeMillis() - timeMillis));
    //        }

    //        {
    //            long timeMillis = System.currentTimeMillis();
    //            Gson gson = new Gson();
    //            for (int i = 0; i < 10000; i++) {
    //                BasicContact contact = new BasicContact();
    //                contact.address = "john" + i + "@example.com";
    //                contact.number = "#" + i;
    //                contact.active = true;
    //                gson.toJson(contact);
    //            }
    //            Log.e("IntroExampleActivity", "toJson Time#: " + (System.currentTimeMillis() -
    // timeMillis));
    //        }

    //        {
    //            long timeMillis = System.currentTimeMillis();
    //            Gson gson = new Gson();
    //            for (int i = 0; i < 10000; i++) {
    //                gson.fromJson("{\"address\":\"john" + i + "@example.com\",\"number\":\"#" + i
    // + "\",\"active\":true}", BasicContact.class);
    //            }
    //            Log.e("IntroExampleActivity", "fromJson Time#: " + (System.currentTimeMillis() -
    // timeMillis));
    //        }

    //        {
    //            long timeMillis = System.currentTimeMillis();
    //            realm.beginTransaction();
    //            for (int i = 0; i < 10000; i++) {
    //                RealmContact contact = realm.createObject(RealmContact.class);
    //                contact.setAddress("john" + i + "@example.com");
    //                contact.setNumber("#" + i);
    //                contact.setActive(true);
    //            }
    //            realm.commitTransaction();
    //            Log.e("IntroExampleActivity", "Realm createObject Time#: " +
    // (System.currentTimeMillis() - timeMillis));
    //        }

    //        {
    //            long timeMillis = System.currentTimeMillis();
    //            realm.beginTransaction();
    //            for (int i = 0; i < 10000; i++) {
    //                RealmContact contact = new RealmContact();
    //                contact.setAddress("john" + i + "@example.com");
    //                contact.setNumber("#" + i);
    //                contact.setActive(true);
    //                realm.copyToRealm(contact);
    //            }
    //            realm.commitTransaction();
    //            Log.e("IntroExampleActivity", "Realm copyToRealm Time#: " +
    // (System.currentTimeMillis() - timeMillis));
    //        }

    {
      long timeMillis = System.currentTimeMillis();
      realm.beginTransaction();
      for (int i = 0; i < 10000; i++) {
        RealmPrimaryContact contact = new RealmPrimaryContact();
        contact.setAddress("john" + i + "@example.com");
        contact.setNumber("#" + i);
        contact.setActive(true);
        realm.copyToRealmOrUpdate(contact);
      }
      realm.commitTransaction();
      Log.e(
          "IntroExampleActivity",
          "Realm copyToRealmOrUpdate Time#: " + (System.currentTimeMillis() - timeMillis));
    }
  }
 public void save(Collection<DoctorLocation> locations) {
   realm.beginTransaction();
   realm.copyToRealmOrUpdate(locations);
   realm.commitTransaction();
 }