I have an object which I for now save using SharedPreferences like this:
我有一个对象,我现在使用SharedPreferences这样保存:
public String getActiveTripString(Context con) {
return preferences.getString(ACTIVE_TRIP, "-1");
}
public void setActiveTripString(Context context, String string) {
setString(context, string, ACTIVE_TRIP);
}
public PSTrip getActiveTrip(Context context) {
String active = getActiveTripString(context);
PSTrip psTrip = null;
if (active.contentEquals("-1")) {
return null;
} else {
try{
psTrip = JsonUtil.jsonToObject(active, PSTrip.class);
}catch (Exception e){
Log.i("","getActiveTrip error is:" + e.getMessage());
}
return psTrip;
}
}
public void setActiveTrip(PSTrip psTrip, Context context) {
try{
setActiveTripString(context, JsonUtil.objectToJson(psTrip, PSTrip.class));
}catch (Exception e){
Log.i("","setActiveTrip error is:" + e.getMessage());
}
}
Where I have function as you can see, that create a json and then save it as a string in SharedPrefferences. But The object is BIG, and the more I add into it, the app start to be more laggy until it's unresponsive. I also need to use this object in a lot of places, so I always need to call: GetActiveTrip to get it, make my modifications, then SetActiveTrip to save it. I'm looking for a more efficient way to save it. I tried with REALM, to save it in a database, but because my object is so big, and modified in a lot of places, I did not quite manage to make it work, Having to call Realm a lot just to add items in the database, in order to have managed database items, so I can add them in my object, and so on. Which also I think might be memory consuming. And It crashes a lot with realm exceptions. Any other way I could do this? Is saving to a file more efficient than to Shared Preferences? As I saw in Android Monitor, analysing my traces, the GSON function that creates the JSON takes a lot of resources. Any suggestions what I could use?
在我可以看到的函数中,创建一个json然后将其保存为SharedPrefferences中的字符串。但是对象很大,而且我加入的越多,应用程序开始变得更加迟钝,直到它没有响应。我还需要在很多地方使用这个对象,所以我总是需要调用:GetActiveTrip来获取它,进行修改,然后调用SetActiveTrip来保存它。我正在寻找一种更有效的方法来保存它。我尝试使用REALM,将其保存在数据库中,但由于我的对象太大了,并且在很多地方进行了修改,我还是没有完全成功地工作,只需要调用Realm就可以在数据库,为了拥有托管数据库项,所以我可以在我的对象中添加它们,依此类推。我认为这可能是内存消耗。并且它与领域异常崩溃很多。我能用其他任何方式吗?保存到文件比共享首选项更有效吗?正如我在Android Monitor中看到的,分析我的跟踪,创建JSON的GSON函数需要大量资源。有什么建议我可以使用吗?
EDIT: My object:
编辑:我的对象:
public class PSTrip extends RealmObject {
private boolean valid;
private String type;
private String travel_mode;
@PrimaryKey
private String id;
private Owner_data owner_data;
private int distance;
private String name;
private double checkinLat;
private double checkinLng;
private double checkoutLng;
private double checkoutLat;
private String icon;
private String status;
private Destination destination;
private int checkout_time;
private int checkin_time;
private Route route;
private String owner;
private String vehicle;
private Flight flight;
@SerializedName("last_updated")
private int lastUpdated;
@SerializedName("steps")
private RealmList<TripStep> tripSteps;
private RealmList<Record> records;
@SerializedName("planned_route")
private Planned_Route plannedRoute;
private Group group;
private float emissions;
private Co2PerKm co2_per_km;
private int update_interval;
private boolean isRoaming = false;
public boolean getIsRoaming() {
return isRoaming;
}
public void setIsRoaming(boolean isRoaming) {
this.isRoaming = isRoaming;
}
public Group getGroup() {
return group;
}
public void setGroup(Group group) {
this.group = group;
}
public int getLastUpdated() {
return lastUpdated;
}
public void setLastUpdated(int lastUpdated) {
this.lastUpdated = lastUpdated;
}
public RealmList<TripStep> getTripSteps() {
return tripSteps;
}
public void setTripSteps(RealmList<TripStep> steps) {
this.tripSteps = steps;
}
public String getVehicle() {
return vehicle;
}
public void setVehicle(String vehicle) {
this.vehicle = vehicle;
}
public Flight getFlight() {
return flight;
}
public void setFlight(Flight flight) {
this.flight = flight;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getTravel_mode() {
return travel_mode;
}
public void setTravel_mode(String travel_mode) {
this.travel_mode = travel_mode;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public Owner_data getOwner_data() {
return owner_data;
}
public void setOwner_data(Owner_data owner_data) {
this.owner_data = owner_data;
}
public int getDistance() {
return distance;
}
public void setDistance(int distance) {
this.distance = distance;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getIcon() {
return icon;
}
public void setIcon(String icon) {
this.icon = icon;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public Destination getDestination() {
return destination;
}
public void setDestination(Destination destination) {
this.destination = destination;
}
public int getCheckout_time() {
return checkout_time;
}
public void setCheckout_time(int checkout_time) {
this.checkout_time = checkout_time;
}
public int getCheckin_time() {
return checkin_time;
}
public void setCheckin_time(int checkin_time) {
this.checkin_time = checkin_time;
}
public Route getRoute() {
return route;
}
public void setRoute(Route route) {
this.route = route;
}
public String getOwner() {
return owner;
}
public void setOwner(String owner) {
this.owner = owner;
}
public PSTrip() {
}
public PSTrip(String id) {
this.id = id;
}
public double getCheckoutLng() {
return checkoutLng;
}
public void setCheckoutLng(double checkoutLng) {
this.checkoutLng = checkoutLng;
}
public double getCheckinLat() {
return checkinLat;
}
public void setCheckinLat(double checkinLat) {
this.checkinLat = checkinLat;
}
public double getCheckinLng() {
return checkinLng;
}
public void setCheckinLng(double checkinLng) {
this.checkinLng = checkinLng;
}
public double getCheckoutLat() {
return checkoutLat;
}
public void setCheckoutLat(double checkoutLat) {
this.checkoutLat = checkoutLat;
}
public boolean isRoaming() {
return isRoaming;
}
public void setRoaming(boolean isRoaming) {
this.isRoaming = isRoaming;
}
public Planned_Route getPlannedRoute() {
return plannedRoute;
}
public void setPlannedRoute(Planned_Route plannedRoute) {
this.plannedRoute = plannedRoute;
}
public boolean isValid() {
return valid;
}
public float getEmissions() {
return emissions;
}
public void setEmissions(float emissions) {
this.emissions = emissions;
}
public Co2PerKm getCo2_per_km() {
return co2_per_km;
}
public void setCo2_per_km(Co2PerKm co2_per_km) {
this.co2_per_km = co2_per_km;
}
public void setValid(boolean valid) {
this.valid = valid;
}
public int getUpdate_interval() {
return update_interval;
}
public void setUpdate_interval(int update_interval) {
this.update_interval = update_interval;
}
public RealmList<Record> getRecords() {
return records;
}
public void setRecords(RealmList<Record> records) {
this.records = records;
}
}
Where: Route, Destination are the google maps object, if you are familiar with them, you know what they include and their size; TripStep = similar with the STEP object from google BUT, it includes 2 arrays:
其中:路线,目的地是谷歌地图对象,如果您熟悉它们,您就会知道它们包含的内容和大小; TripStep =与Google BUT中的STEP对象类似,它包含2个数组:
private RealmList<StopInfo> filteredLocations = new RealmList<>();
private RealmList<StopInfo> rawLocations = new RealmList<>();
In which I have to add a new location every 5-6 seconds in the rawLocations. Add a new location each time the rawlocation I get is farther than x metres from the last rawLocation I got. Getting the Object from Preferences, deserialising, getting the latest TripStep and adding the new Location to the filteredLocations and rawLocations seems to take a log of memory. So This is what I think is the problem
我必须在rawLocations中每5-6秒添加一个新位置。每次我获得的rawlocation比我上次获得的rawLocation的x米远时添加一个新位置。从首选项获取对象,反序列化,获取最新的TripStep并将新位置添加到filteredLocations和rawLocations似乎记录内存。所以这就是我认为的问题
1 个解决方案
#1
0
After all, I chose to use Realm, instead of shared preferences, it's more efficient, and even if I still have some issues with the changes I make to my file, I'm getting to a stable version quickly. It has it's downsides (some REALM objects are not serialised correctly by GSON, and you will need to use jackson, and also not being able to use functions inside your model, or that it supports just primitives is a big issue with it, but if you manage to go over this, it's worth it) Would not suggest adding Realm database to a project that is already big
毕竟,我选择使用Realm,而不是共享首选项,它更有效率,即使我对我的文件所做的更改仍有一些问题,我也很快就会找到稳定的版本。它有它的缺点(一些REALM对象没有被GSON正确序列化,你需要使用jackson,也不能使用你的模型中的函数,或者它只支持原语是一个很大的问题,但如果你设法回过头来看,这是值得的)不建议将Realm数据库添加到已经很大的项目中
#1
0
After all, I chose to use Realm, instead of shared preferences, it's more efficient, and even if I still have some issues with the changes I make to my file, I'm getting to a stable version quickly. It has it's downsides (some REALM objects are not serialised correctly by GSON, and you will need to use jackson, and also not being able to use functions inside your model, or that it supports just primitives is a big issue with it, but if you manage to go over this, it's worth it) Would not suggest adding Realm database to a project that is already big
毕竟,我选择使用Realm,而不是共享首选项,它更有效率,即使我对我的文件所做的更改仍有一些问题,我也很快就会找到稳定的版本。它有它的缺点(一些REALM对象没有被GSON正确序列化,你需要使用jackson,也不能使用你的模型中的函数,或者它只支持原语是一个很大的问题,但如果你设法回过头来看,这是值得的)不建议将Realm数据库添加到已经很大的项目中