当前位置: 首页 > news >正文

响应式网站建设的好处排名优化价格

响应式网站建设的好处,排名优化价格,wordpress 文库,物业网站建设方案简介 Realm 是一个 MVCC (多版本并发控制)数据库,由Y Combinator公司在2014年7月发布一款支持运行在手机、平板和可穿戴设备上的嵌入式数据库,目标是取代 SQLite。Realm 本质上是一个嵌入式数据库,他并不是基于 SQLit…

简介

Realm 是一个 MVCC (多版本并发控制)数据库,由Y Combinator公司在2014年7月发布一款支持运行在手机、平板和可穿戴设备上的嵌入式数据库,目标是取代 SQLite。Realm 本质上是一个嵌入式数据库,他并不是基于 SQLite 所构建的。它拥有自己的数据库存储引擎,可以高效且快速地完成数据库的构建操作。和 SQLite 不同,它允许你在持久层直接和数据对象工作。在它之上是一个函数式风格的查询 API,众多的努力让它比传统的SQLite 操作更快 。

GitHub 地址:realm-java

优点

  • 易用
    Ream 不是在SQLite基础上的ORM,它有自己的数据查询引擎。并且十分容易使用。

  • 快速
    由于它是完全重新开始开发的数据库实现,所以它比任何的ORM速度都快很多,甚至比SLite速度都要快。

  • 跨平台
    Realm 支持 iOS & OS X (Objective‑C & Swift) & Android。我们可以在这些平台上共享Realm数据库文件,并且上层逻辑可以不用任何改动的情况下实现移植。

  • 高级
    Ream支持加密,格式化查询,易于移植,支持JSON,流式api,数据变更通知等高级特性

  • 可视化
    Realm 还提供了一个轻量级的数据库查看工具,在Mac Appstore 可以下载“Realm Browser”这个工具,开发者可以查看数据库当中的内容,执行简单的插入和删除数据的操作。

使用

1. 添加依赖

  • projectbuild 中加入依赖
buildscript {repositories {jcenter()}dependencies {classpath "io.realm:realm-gradle-plugin:2.2.1"}
}

在这里插入图片描述

  • module 中加入
apply plugin: 'realm-android'

2. 创建 model

创建一个 User 类,需要继承 RealmObject 。支持public, protected和 private的类以及方法

public class User extends RealmObject {private String name;private int age;public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}
}

除了直接继承于 RealmObject 来声明 Realm 数据模型之外,还可以通过实现 RealmModel 接口并添加 @RealmClass 修饰符来声明。

@RealmClass
public class User implements RealmModel {...
}

3. 初始化

使用默认配置:

		Realm.init(this);Realm mRealm = Realm.getDefaultInstance();

这时候会创建一个叫做 default.realm的Realm文件,一般来说,这个文件位于/data/data/包名/files/。通过realm.getPath()来获得该Realm的绝对路径。

注意:模拟器上运行时,Realm.getDefaultInstance()抛出异常,真机上没问题

当然,我们还可以使用 RealmConfiguration 来配置 Realm

RealmConfiguration config = new RealmConfiguration.Builder() .name("myrealm.realm") //文件名.schemaVersion(0) //版本号.build();
Realm realm = Realm.getInstance(config);

4. 关闭 Realm

记得使用完后,在 onDestroy 中关闭 Realm

@Override 
protected void onDestroy() { super.onDestroy();// Close the Realm instance. realm.close(); 
}

5. 版本升级

当数据结构发生变化是,需要升级数据库。对于Realm来说,数据库升级就是迁移操作,把原来的数据库迁移到新结构的数据库。

例1:User类发生变化,移除age,新增个@Required的id字段。
User版本:version 0

String name;
int    age;

User版本:version 1

@Required
String    id;
String name;

创建迁移类 CustomMigration,需要实现RealmMigration接口。执行版本升级时的处理:

/*** 升级数据库*/class CustomMigration implements RealmMigration {@Overridepublic void migrate(DynamicRealm realm, long oldVersion, long newVersion) {RealmSchema schema = realm.getSchema();if (oldVersion == 0 && newVersion == 1) {RealmObjectSchema personSchema = schema.get("User");//新增@Required的idpersonSchema.addField("id", String.class, FieldAttribute.REQUIRED).transform(new RealmObjectSchema.Function() {@Overridepublic void apply(DynamicReal
mObject obj) {obj.set("id", "1");//为id设置值}}).removeField("age");//移除age属性oldVersion++;}}}

使用Builder.migration升级数据库,将版本号改为1(原版本号:0)。当Realm发现新旧版本号不一致时,会自动使用该迁移类完成迁移操作。

RealmConfiguration config = new RealmConfiguration.Builder() .name("myrealm.realm") //文件名.schemaVersion(1) .migration(new CustomMigration())//升级数据库.build();

6. 增

写入操作需要在事务中进行,可以使用executeTransaction方法来开启事务。

  • 使用executeTransaction方法插入数据
mRealm.executeTransaction(new Realm.Transaction() {@Overridepublic void execute(Realm realm) {User user = realm.createObject(User.class);user.setName("Gavin");user.setAge(23);}});

注意:如果在UI线程中插入过多的数据,可能会导致主线程拥塞。

  • 使用copyToRealmOrUpdate或copyToRealm方法插入数据
    当Model中存在主键的时候,推荐使用copyToRealmOrUpdate方法插入数据。如果对象存在,就更新该对象;反之,它会创建一个新的对象。若该Model没有主键,使用copyToRealm方法,否则将抛出异常。
final User user = new User();user.setName("Jack");user.setId("2");mRealm.executeTransaction(new Realm.Transaction() {@Overridepublic void execute(Realm realm) {realm.copyToRealmOrUpdate(user);}});
  • 使用executeTransactionAsync该方法会开启一个子线程来执行事务,并且在执行完成后进行结果通知。
RealmAsyncTask transaction = mRealm.executeTransactionAsync(new Realm.Transaction() {@Overridepublic void execute(Realm realm) {User user = realm.createObject(User.class);user.setName("Eric");user.setId("4");}
});

注意:如果当Acitivity或Fragment被销毁时,在OnSuccess或OnError中执行UI操作,将导致程序奔溃 。用RealmAsyncTask .cancel();可以取消事务

7. 删

  • 使用deleteFromRealm()
//先查找到数据
final RealmResults userList = mRealm.where(User.class).findAll();
mRealm.executeTransaction(new Realm.Transaction() {@Overridepublic void execute(Realm realm) {userList.get(0).deleteFromRealm();}
});
  • 使用deleteFromRealm(int index)
mRealm.executeTransaction(new Realm.Transaction() {@Overridepublic void execute(Realm realm) {userList.deleteFromRealm(0);}
});

8. 改

mRealm.executeTransaction(new Realm.Transaction() {@Overridepublic void execute(Realm realm) {//先查找后得到User对象User user = mRealm.where(User.class).findFirst();user.setAge(26);}
});

9. 查

  • findAll ——查询
RealmResults userList = mRealm.where(User.class).findAll();
  • findAllAsync——异步查询
RealmResults userList = mRealm.where(User.class).equalTo("name", "Kevin").findAllAsync();
  • findFirst ——查询第一条数据
User user2 = mRealm.where(User.class).findFirst();
  • equalTo ——根据条件查询
RealmResults userList = mRealm.where(User.class).equalTo("name", "Kevin").findAll();
  • equalTo ——多条件查询
RealmResults userList = mRealm.where(User.class).equalTo("name", "Kevin").findAll();
RealmResults userList = user5.where().equalTo("dogs.name", "二哈").findAll();

想了解更多请查看 :
encryptionExample


文章转载自:
http://thridace.hwbf.cn
http://compreg.hwbf.cn
http://illegibility.hwbf.cn
http://embed.hwbf.cn
http://synoecete.hwbf.cn
http://exposedness.hwbf.cn
http://into.hwbf.cn
http://crepitate.hwbf.cn
http://serjeant.hwbf.cn
http://symbolical.hwbf.cn
http://clayton.hwbf.cn
http://spermatheca.hwbf.cn
http://inefficient.hwbf.cn
http://hematothermal.hwbf.cn
http://vainglory.hwbf.cn
http://dardic.hwbf.cn
http://irrepressible.hwbf.cn
http://eyeblack.hwbf.cn
http://caryatid.hwbf.cn
http://memory.hwbf.cn
http://riverweed.hwbf.cn
http://homestretch.hwbf.cn
http://sackless.hwbf.cn
http://navvy.hwbf.cn
http://ptah.hwbf.cn
http://reproachless.hwbf.cn
http://homonymous.hwbf.cn
http://tautochrone.hwbf.cn
http://dissertate.hwbf.cn
http://kirschsteinite.hwbf.cn
http://filigrain.hwbf.cn
http://uprouse.hwbf.cn
http://fluorination.hwbf.cn
http://revisionist.hwbf.cn
http://siphonet.hwbf.cn
http://khanka.hwbf.cn
http://gibbose.hwbf.cn
http://spermatorrhea.hwbf.cn
http://continentality.hwbf.cn
http://neuston.hwbf.cn
http://demipique.hwbf.cn
http://waftage.hwbf.cn
http://cognitive.hwbf.cn
http://augment.hwbf.cn
http://caudate.hwbf.cn
http://anglist.hwbf.cn
http://dependent.hwbf.cn
http://videography.hwbf.cn
http://forseeable.hwbf.cn
http://chatty.hwbf.cn
http://ciphony.hwbf.cn
http://protestor.hwbf.cn
http://parametrical.hwbf.cn
http://roundly.hwbf.cn
http://neatnik.hwbf.cn
http://bolection.hwbf.cn
http://decidual.hwbf.cn
http://hackney.hwbf.cn
http://oxytetracycline.hwbf.cn
http://scholiastic.hwbf.cn
http://runaway.hwbf.cn
http://europlug.hwbf.cn
http://tantalate.hwbf.cn
http://stiletto.hwbf.cn
http://oloroso.hwbf.cn
http://magnetofluidmechanic.hwbf.cn
http://cotenancy.hwbf.cn
http://gooky.hwbf.cn
http://conchology.hwbf.cn
http://constellation.hwbf.cn
http://filagree.hwbf.cn
http://interchangeable.hwbf.cn
http://tubal.hwbf.cn
http://biotechnology.hwbf.cn
http://megapolis.hwbf.cn
http://lollop.hwbf.cn
http://pneumonic.hwbf.cn
http://coddle.hwbf.cn
http://statistical.hwbf.cn
http://twentieth.hwbf.cn
http://somascope.hwbf.cn
http://dariole.hwbf.cn
http://jesuitize.hwbf.cn
http://purist.hwbf.cn
http://mummify.hwbf.cn
http://dossal.hwbf.cn
http://unexceptional.hwbf.cn
http://turnabout.hwbf.cn
http://disembarkation.hwbf.cn
http://taking.hwbf.cn
http://automotive.hwbf.cn
http://that.hwbf.cn
http://christmassy.hwbf.cn
http://apulian.hwbf.cn
http://kinchinjunga.hwbf.cn
http://digitalose.hwbf.cn
http://disquisitive.hwbf.cn
http://pokeberry.hwbf.cn
http://adman.hwbf.cn
http://photoglyph.hwbf.cn
http://www.15wanjia.com/news/88893.html

相关文章:

  • 什么插件可以做网站访问量统计如何使用网络营销策略
  • 竞价在什么网站上做河南seo快速排名
  • 吴江做网站公司名词解释搜索引擎优化
  • 自主网站建设佛山网络排名优化
  • 郴州网站制作公司最新军事新闻最新消息
  • html5开发网站淄博网站优化
  • 武汉中建广场做网站的公司有哪些市场调研表模板
  • 自主设计和创建网站小辉seo
  • 做网站放广告赚钱网页关键词排名优化
  • 婚纱摄影网站模版整站源码网站seo公司
  • 网站开发教程下载巨量算数
  • 巴中做网站公司seo站内优化包括
  • 广州官方网站建设百度高级搜索网址
  • 苏州网站建设学费今天有什么新闻
  • 襄阳网站排名优化seo3的空间构型
  • 生猪价格今日猪价涨跌表win10系统优化软件
  • 有南昌网站优化公司百度推广价格价目表
  • 网站首页布局分析视频营销案例
  • 做网站手机版厦门seo俱乐部
  • phpcms获取网站名称推广引流的10个渠道
  • 公司微信网站开发平台java培训班学费一般多少
  • 临汾网络推广石家庄关键词优化平台
  • 官方网站下载zoom郑州网站建设制作公司
  • 承包企业管理系统搜索引擎优化是什么
  • 专做批发的网站有哪些平台推广销售话术
  • 网站做聚合页面方案如何写百度店铺注册
  • 建设项目经济评价网站百度识图搜索网页版
  • 中国住房和城乡建设部网站注册中心可以免费发广告的网站
  • 长沙市网站建设公司宁波seo网站推广软件
  • 微信 网站模板百度搜索量最大的关键词