Java Code Examples for io.realm.Realm#copyToRealm()

The following examples show how to use io.realm.Realm#copyToRealm() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ApiImpl.java    From Theogony with MIT License 6 votes vote down vote up
@Override
public boolean writeToRealmDataBase(DragonData dragonData) {
    if (dragonData == null) return false;

    Realm realm = RealmProvider.getInstance().getRealm();
    try {
        realm.beginTransaction();
        realm.deleteAll();
        realm.copyToRealm(dragonData);
        realm.commitTransaction();
        return true;
    } catch (Exception e) {
        LogHelper.LOGW("数据写入失败:" + e);
    } finally {
        realm.close();
    }
    return false;
}
 
Example 2
Source File: MeiziUtil.java    From MoeQuest with Apache License 2.0 6 votes vote down vote up
/**
 * 保存gank妹子到数据库中
 */

public void putGankMeiziCache(List<GankMeiziInfo> gankMeiziInfos) {

  GankMeizi meizi;
  Realm realm = Realm.getDefaultInstance();
  realm.beginTransaction();
  for (int i = 0; i < gankMeiziInfos.size(); i++) {
    meizi = new GankMeizi();
    String url = gankMeiziInfos.get(i).url;
    String desc = gankMeiziInfos.get(i).desc;
    meizi.setUrl(url);
    meizi.setDesc(desc);
    realm.copyToRealm(meizi);
  }
  realm.commitTransaction();
  realm.close();
}
 
Example 3
Source File: RealmTester.java    From AndroidDatabaseLibraryComparison with MIT License 6 votes vote down vote up
public static void testAddressItems(Context context) {
    Realm realm = Realm.getDefaultInstance();
    realm.beginTransaction();
    realm.where(SimpleAddressItem.class).findAll().deleteAllFromRealm();
    realm.commitTransaction();

    Collection<SimpleAddressItem> modelList = Generator.getAddresses(SimpleAddressItem.class, MainActivity.LOOP_COUNT);
    long startTime = System.currentTimeMillis();
    realm.beginTransaction();
    realm.copyToRealm(modelList);
    realm.commitTransaction();
    EventBus.getDefault().post(new LogTestDataEvent(startTime, FRAMEWORK_NAME, MainActivity.SAVE_TIME));

    startTime = System.currentTimeMillis();
    modelList = realm.where(SimpleAddressItem.class).findAll();
    EventBus.getDefault().post(new LogTestDataEvent(startTime, FRAMEWORK_NAME, MainActivity.LOAD_TIME));

    realm.beginTransaction();
    realm.where(SimpleAddressItem.class).findAll().deleteAllFromRealm();
    realm.commitTransaction();

    realm.close();
}
 
Example 4
Source File: IntegrationsManager.java    From HAPP with GNU General Public License v3.0 6 votes vote down vote up
public static void newCarbs(Carb carb, Realm realm){
    SharedPreferences prefs =   PreferenceManager.getDefaultSharedPreferences(MainApp.instance());
    String happ_object      =   "treatment_carbs";

    // NS Integrations
    if (NSUploader.isNSIntegrationActive("nightscout_treatments", prefs)) {
        if (carb != null) {
            Integration carbIntegration = new Integration("ns_client", happ_object, carb.getId());
            carbIntegration.setState        ("to sync");
            carbIntegration.setAction       ("new");
            carbIntegration.setRemote_var1  ("carbs");
            realm.beginTransaction();
            realm.copyToRealm(carbIntegration);
            realm.commitTransaction();
        }
    }

    Log.d(TAG, "newCarbs");
    syncIntegrations(MainApp.instance(), realm);
}
 
Example 5
Source File: NotesFragment.java    From multi-copy with Apache License 2.0 5 votes vote down vote up
@Override
public void onNotesEdit(String notes, int position) {
    if (notesFragment_LOG)
    Log.d(TAG, "onNotesEdit: " + notes);
    Realm realm = Realm.getDefaultInstance();
    realm.beginTransaction();
    notesList.get(position).setNote(notes);
    realm.copyToRealm(notesList.get(position));
    notesAdapter.notifyItemChanged(position);
    realm.commitTransaction();
}
 
Example 6
Source File: RealmOfflineEdited.java    From iGap-Android with GNU Affero General Public License v3.0 5 votes vote down vote up
public static RealmOfflineEdited put(Realm realm, long messageId, String message) {
    RealmOfflineEdited realmOfflineEdited = realm.createObject(RealmOfflineEdited.class, SUID.id().get());
    realmOfflineEdited.setMessageId(messageId);
    realmOfflineEdited.setMessage(message);
    realmOfflineEdited = realm.copyToRealm(realmOfflineEdited);

    return realmOfflineEdited;
}
 
Example 7
Source File: RealmMember.java    From iGap-Android with GNU Affero General Public License v3.0 5 votes vote down vote up
public static RealmMember put(Realm realm, long userId, String role) {
    RealmMember realmMember = realm.createObject(RealmMember.class, SUID.id().get());
    realmMember.setRole(role);
    realmMember.setPeerId(userId);
    realmMember = realm.copyToRealm(realmMember);
    return realmMember;
}
 
Example 8
Source File: RealmMember.java    From iGap-Android with GNU Affero General Public License v3.0 5 votes vote down vote up
public static RealmMember put(Realm realm, ProtoGroupGetMemberList.GroupGetMemberListResponse.Member member) {
    RealmMember realmMember = realm.createObject(RealmMember.class, SUID.id().get());
    realmMember.setRole(member.getRole().toString());
    realmMember.setPeerId(member.getUserId());
    realmMember = realm.copyToRealm(realmMember);
    return realmMember;
}
 
Example 9
Source File: RealmMember.java    From iGap-Android with GNU Affero General Public License v3.0 5 votes vote down vote up
public static RealmMember put(Realm realm, ProtoChannelGetMemberList.ChannelGetMemberListResponse.Member member) {
    RealmMember realmMember = realm.createObject(RealmMember.class, SUID.id().get());
    realmMember.setRole(member.getRole().toString());
    realmMember.setPeerId(member.getUserId());
    realmMember = realm.copyToRealm(realmMember);
    return realmMember;
}
 
Example 10
Source File: DatabaseRealm.java    From openwebnet-android with MIT License 5 votes vote down vote up
public <T extends RealmObject> T add(T model) {
    Realm realm = getRealmInstance();
    realm.beginTransaction();
    realm.copyToRealm(model);
    realm.commitTransaction();
    return model;
}
 
Example 11
Source File: DatabaseRealm.java    From openwebnet-android with MIT License 5 votes vote down vote up
public <T extends RealmObject> Iterable<T> addAll(Iterable<T> models) {
    Realm realm = getRealmInstance();
    realm.beginTransaction();
    realm.copyToRealm(models);
    realm.commitTransaction();
    return models;
}
 
Example 12
Source File: MeizhiFetchingService.java    From GankMeizhi with Apache License 2.0 5 votes vote down vote up
/**
 * 预解码图片并将抓到的数据保存至数据库
 *
 * @param realm Realm 实例
 * @param image 图片
 * @return 是否保存成功
 */
private boolean saveToDb(Realm realm, Image image) {
    realm.beginTransaction();

    try {
        realm.copyToRealm(Image.persist(image, this));
    } catch (IOException | InterruptedException | ExecutionException e) {
        Log.e(TAG, "Failed to fetch image", e);
        realm.cancelTransaction();
        return false;
    }

    realm.commitTransaction();
    return true;
}
 
Example 13
Source File: xDripIncoming.java    From HAPP with GNU General Public License v3.0 5 votes vote down vote up
public static void New_data(Intent intent, Realm realm){
    Log.d(TAG, "New xDrip Broadcast Received");

    if (intent == null) return;
    Bundle bundle = intent.getExtras();
    if (bundle == null) return;

    final double bgEstimate = bundle.getDouble(Intents.EXTRA_BG_ESTIMATE,0);
    if (bgEstimate == 0) return;

    final Bg bg = new Bg();
    bg.setDirection     (bundle.getString(Intents.EXTRA_BG_SLOPE_NAME));
    bg.setBattery       (bundle.getInt(Intents.EXTRA_SENSOR_BATTERY));
    bg.setBgdelta       (bundle.getDouble(Intents.EXTRA_BG_SLOPE, 0) * 1000 * 60 * 5);
    bg.setDatetime      (new Date(bundle.getLong(Intents.EXTRA_TIMESTAMP, new Date().getTime())));
    bg.setSgv           (Integer.toString((int) bgEstimate, 10));

    realm.beginTransaction();
    realm.copyToRealm(bg);
    realm.commitTransaction();

    Log.d(TAG, "New BG saved, sending out UI Update");

    Intent updateIntent = new Intent(Intents.UI_UPDATE);
    updateIntent.putExtra("UPDATE", "NEW_BG");
    LocalBroadcastManager.getInstance(MainApp.instance()).sendBroadcast(updateIntent);
}
 
Example 14
Source File: pumpAction.java    From HAPP with GNU General Public License v3.0 5 votes vote down vote up
public static void setTempBasal(TempBasal newTempBasal, Realm realm){
    APSResult apsResult = APSResult.last(realm);
    realm.beginTransaction();
    apsResult.setAccepted(true);
    realm.commitTransaction();

    if (newTempBasal == null) newTempBasal = apsResult.getBasal();

    if (newTempBasal.checkIsCancelRequest()){
        cancelTempBasal(realm);

    } else {

        Profile p = new Profile(new Date());
        Safety safety = new Safety();

        //Sanity check the suggested rate is safe
        if (!safety.checkIsSafeMaxBolus(newTempBasal.getRate())) newTempBasal.setRate(safety.getMaxBasal(p));

        //Save
        newTempBasal.setStart_time(new Date());
        realm.beginTransaction();
        realm.copyToRealm(newTempBasal);
        realm.commitTransaction();

        //Clear notifications
        Notifications.clear("updateCard");
        Notifications.clear("newTemp");

        //Inform Integrations Manager
        IntegrationsManager.newTempBasal(newTempBasal, realm);

        //Update UI
        Intent intentUpdate = new Intent(Intents.UI_UPDATE);
        intentUpdate.putExtra("UPDATE", "NEW_APS_RESULT");
        LocalBroadcastManager.getInstance(MainApp.instance()).sendBroadcast(intentUpdate);

    }
}
 
Example 15
Source File: pumpAction.java    From HAPP with GNU General Public License v3.0 5 votes vote down vote up
public static void cancelTempBasal(Realm realm){
    //Cancels a Running Temp Basal and updates the DB with the Temp Basal new duration
    final TempBasal active_basal = TempBasal.getCurrentActive(null, realm);

    if (active_basal.isactive(null)) {

        //stop the temp basal
        realm.beginTransaction();
        active_basal.setDuration(active_basal.age());
        realm.copyToRealm(active_basal);
        realm.commitTransaction();

        //Inform Integrations Manager
        IntegrationsManager.cancelTempBasal(active_basal, realm);

        Notifications.newInsulinUpdate(realm);

        //Update Main Activity of Current Temp Change
        Intent intent = new Intent(Intents.UI_UPDATE);
        intent.putExtra(Constants.UPDATE, Constants.broadcast.UPDATE_RUNNING_TEMP);
        LocalBroadcastManager.getInstance(MainApp.instance()).sendBroadcast(intent);

        //Update UI
        Intent intentUpdate = new Intent(Intents.UI_UPDATE);
        intentUpdate.putExtra(Constants.UPDATE, Constants.broadcast.NEW_APS_RESULT);
        LocalBroadcastManager.getInstance(MainApp.instance()).sendBroadcast(intentUpdate);

    } else {
        //No temp Basal active
        Toast.makeText(MainApp.instance(), MainApp.instance().getString(R.string.pump_action_no_active_tbr), Toast.LENGTH_LONG).show();
    }
}