我有一個對象,我現在保存使用SharedPreferences是這樣的:我怎樣纔能有效地保存對象中的Android
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());
}
}
在那裏我有功能,你可以看到,創建一個JSON,然後保存作爲SharedPrefferences中的字符串。但是,該對象是BIG,我添加的內容越多,該應用程序開始變得越遲緩,直到它沒有響應。 我也需要在很多地方使用這個對象,所以我總是需要撥打: GetActiveTrip來獲取它,進行我的修改,然後SetActiveTrip來保存它。 我正在尋找一種更有效的方式來保存它。我嘗試過使用REALM,將它保存在數據庫中,但由於我的對象非常大,並且在很多地方進行了修改,所以我沒有完全設法使其工作,不得不打電話給Realm,只是爲了添加項目數據庫,爲了管理數據庫項目,所以我可以將它們添加到我的對象中,等等。我認爲這也可能是內存消耗。而且它通過領域異常崩潰了很多。 任何其他方式我可以做到這一點?保存到文件比共享首選項更有效嗎?正如我在Android Monitor中看到的,分析我的跟蹤信息,創建JSON的GSON函數佔用了大量資源。 有什麼建議我可以使用?
編輯:我的目標:
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;
}
}
其中:路線,目的地是谷歌地圖對象,如果你熟悉他們,你知道他們有和他們的大小; TripStep =與從谷歌的STEP對象,但類似,它包括2個陣列:
private RealmList<StopInfo> filteredLocations = new RealmList<>();
private RealmList<StopInfo> rawLocations = new RealmList<>();
在我所添加新的位置每隔5-6秒的rawLocations。 每當我得到的原始位置比距離我獲得的最後一個rawLocation x米遠時,添加一個新位置。 從首選項獲取對象,反序列化,獲取最新的TripStep並將新的Location添加到filteredLocations和rawLocations似乎需要記錄內存。所以這就是我認爲的問題
「BIG」有多大? –
@ci_我做了一個編輯,包括我的對象的模型。我應該爲我的其他物體包含模型嗎?就像我說的,Route,Destination是來自google。 Tripstep是谷歌STEP類的一個修改版本,其餘的都比較小。但如果有必要,我可以包括它們。 但我認爲問題出在TripStep –