这是我的RealmObject类的模型:
public class ARDatabase extends RealmObject
{
@PrimaryKey
private String uid;
private String namex;
private String desc;
private boolean isVideo;
private boolean isDeleted;
private String urlImg;
private String urlApp;
private int updates;
private boolean isDownloaded;
private String location;
public ARDatabase(){}
public String getUid()
{
return uid;
}
public void setUid(String uid)
{
this.uid = uid;
}
public String getNamex()
{
return namex;
}
public void setNamex(String namex)
{
this.namex = namex;
}
public String getDesc()
{
return desc;
}
public void setDesc(String desc)
{
this.desc = desc;
}
public boolean getIsVideo()
{
return isVideo;
}
public void setIsVideo(boolean isVideo)
{
this.isVideo = isVideo;
}
public boolean getIsDeleted()
{
return isDeleted;
}
public void setIsDeleted(boolean isDeleted)
{
this.isDeleted = isDeleted;
}
public String getUrlImg()
{
return urlImg;
}
public void setUrlImg(String urlImg)
{
this.urlImg = urlImg;
}
public String getUrlApp()
{
return urlApp;
}
public void setUrlApp(String urlApp)
{
this.urlApp = urlApp;
}
public int getUpdates()
{
return updates;
}
public void setUpdates(int updates)
{
this.updates = updates;
}
public boolean getIsDownloaded()
{
return isDownloaded;
}
public void setIsDownloaded(boolean isDownloaded)
{
this.isDownloaded = isDownloaded;
}
public String getLocation()
{
return location;
}
public void setLocation(String location)
{
this.location = location;
}
}
这就是我从默认领域搜索uid的方式:
final ARDatabase db = mRealm.where(ARDatabase.class).equalTo("uid",imageTitles.get(result)).findFirst();
现在,问题是:考虑到我的领域中有10-100个对象,搜索的速度有多快?
该用例用于图像识别应用程序.当应用识别出图像时,它会返回uid,并且基于uid,我需要在屏幕上提供与uid尽快关联的信息的覆盖图.
现在,由于我有大约10-100个对象,因此线性搜索O(n)或通用二进制搜索O(log n)会比我上面使用的Realm搜索更快吗?还是可以调整Realm以获得更快的结果? (以防它现在无法以最快的方式执行).
解决方法:
在Realm中进行搜索始终会更快,因为您可以在C内核中执行整个搜索.自己进行搜索将意味着您在Java和C之间来回往返会产生开销.
快速搜索单个元素的唯一要求是,您在字段上具有@Index注释,但是对于您而言,您已经具有@PrimaryKey,它也将自动应用@Index注释.
因此,您的查询将尽可能快.此外,对于10-100个对象,无论您执行什么操作,它都可能对用户即时显示.