2015-09-04 74 views
0

我有一个对象,我现在保存使用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似乎需要记录内存。所以这就是我认为的问题

+0

“BIG”有多大? –

+0

@ci_我做了一个编辑,包括我的对象的模型。我应该为我的其他物体包含模型吗?就像我说的,Route,Destination是来自google。 Tripstep是谷歌STEP类的一个修改版本,其余的都比较小。但如果有必要,我可以包括它们。 但我认为问题出在TripStep –

回答

0

毕竟,我选择使用Realm而不是共享首选项,它更有效率,即使我对我的文件所做的更改仍然存在一些问题,快速获得稳定版本。 它有缺点(某些REALM对象没有被GSON正确序列化,并且您需要使用jackson,并且不能在模型中使用函数,或者它只支持基元是它的一个大问题,但如果你设法去做这件事,这是值得的) 不会建议将Realm数据库添加到已经很大的项目中