我正在使用Parse来存储我的云数据。当我启动这个应用程序时,我有一个initializeData()方法来获取我想要在本地存储的数据。例如,我想获取并在本地保存登录用户最喜欢的保姆的列表:
final UzurvUser user = UzurvUser.currentUzurvUser();
user.getFavoriteNannys().getQuery().findInBackground(new FindCallback<UzurvUser>() {
@Override
public void done(final List<UzurvUser> nannys, ParseException e) {
if (e == null) {
UzurvUser.unpinAllInBackground(NANNYS_GROUP_LABEL,
new DeleteCallback() {
public void done(ParseException e) {
if (e == null) {
UzurvUser.pinAllInBackground(NANNYS_GROUP_LABEL, nannys);
} else {
e.printStackTrace();
}
}
});
} else {
e.printStackTrace();
}
}
});然后,我稍后检索它们:
ParseQuery<UzurvUser> query = ParseQuery.getQuery(UzurvUser.class);
query.fromPin(MainActivity.NANNYS_GROUP_LABEL);
mFavoriteNannys = new ArrayList<>(query.find());现在的问题是,如果我想从用户关系中删除一个保姆并将其保存在云中,我将不得不更新保姆列表的本地副本,并保存当前用户:
ParseRelation<UzurvUser> pr = UzurvUser.currentUzurvUser().getFavoriteNannys();
pr.remove(nannyToRemove);
mNanny.unpinInBackground(MainActivity.NANNYS_GROUP_LABEL);
UzurvUser.currentUzurvUser().saveEventually();这是我所描述的同步数据的“正确”方式吗?我不希望每次在用户关系中添加或删除保姆时都必须更新保姆列表并保存用户,但我仍然希望拥有此用户所有保姆的本地副本。
发布于 2015-08-13 02:30:04
如果您希望在本地数据库和服务器这两个位置获得相同的列表,我确实认为您应该发送一个请求,使用AsynTask或其他方法在服务器中添加/更新/删除此项目,然后,当您收到确认此更改已在服务器中执行时,在您的本地数据库中添加/更新/删除您的项目。
https://stackoverflow.com/questions/31972258
复制相似问题