這是我的目標:Java的Android的境界檢查,如果對象是存在的(檢查兩個)
public class ObjectsInGroupRealm extends RealmObject {
@PrimaryKey
private Long id;
private String name;
private String groupName;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGroupName() {
return groupName;
}
public void setGroupName(String groupName) {
this.groupName = groupName;
}
}
當我創建一個新的對象我想檢查對象是存在這個相同的名字和這個相同的組名。對象名稱可能在幾個組中。這是我的代碼,我如何保存對象:
public static void saveObjectsInGroup(ArrayList<String> objects, String groupName , Realm realm){
for(String object : objects){
ObjectsInGroupRealm objectsInGroupRealm = new ObjectsInGroupRealm();
Long key;
try {
key = (Long) realm.where(ObjectsInGroupRealm.class).max("id") + 1;
} catch (NullPointerException ex) {
key = 0L; // when there is no object in the database yet
}
objectsInGroupRealm.setId(key);
objectsInGroupRealm.setName(object);
objectsInGroupRealm.setGroupName(groupName);
realm.beginTransaction();
realm.copyToRealm(objectsInGroupRealm);
realm.commitTransaction();
}
}
什麼是你的問題? – GhostCat
@GhostCat我想檢查是否存在的對象groupName和名稱,我有,當不存在我想保存這個對象 –