gpt4 book ai didi

android - parse.com 调用 .save() 导致所有查询停止运行 Android

转载 作者:搜寻专家 更新时间:2023-10-30 21:59:10 25 4
gpt4 key购买 nike

我有两个对象,一个属于可以投票的交易对象的建立对象。如果我对同一笔交易多次投赞成票/反对票,那么我第七次投票时,查询就会停止,不会做任何事情。该应用程序不会崩溃,但也不会保存。如果我进入另一个需要 parse.com 查询的 Activity ,该查询也将不起作用。这是我的赞成票逻辑(反对票是相同的)。假设所有使用的变量都在 onCreate() 之前初始化。我的查询是否在某处的管道中得到备份?

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
upVoteButton = (Button) findViewById(R.id.deal_up_vote_button);
upVoteButton.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
new UpVoteTask().execute();
}
});
}

// visually changes buttons if they are selected
private void setButtons(Boolean queryDb) {

if (queryDb == true) {
queryParse();
}

// if deal found correctly
if (deal != null) {
// if user found correctly
if (dealVoteUser != null) {
if (dealVoteUser.get("vote").toString().equals("0")) {
upVoteButton.setPressed(false);
downVoteButton.setPressed(true);
} else if (dealVoteUser.get("vote").toString().equals("1")) {
upVoteButton.setPressed(true);
downVoteButton.setPressed(false);
} else if (dealVoteUser.get("vote").toString().equals("2")) {
upVoteButton.setPressed(false);
downVoteButton.setPressed(false);
}
}
}
}

// queries parse and populates vars
private void queryParse(){
ParseQuery<ParseObject> queryDeal = ParseQuery.getQuery("Deal");
queryDeal.whereEqualTo("objectId", deal_id);
try {
deal = queryDeal.getFirst();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

ParseQuery<ParseObject> queryDealVoteUser = ParseQuery
.getQuery("deal_vote_users");
queryDealVoteUser.whereEqualTo("deal", deal).whereEqualTo("user",
ParseUser.getCurrentUser());
try {
dealVoteUser = queryDealVoteUser.getFirst();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

// UpVoteTask AsyncTask
private class UpVoteTask extends AsyncTask<Void, Void, Void> {

@Override
protected void onPreExecute() {
super.onPreExecute();
// Create a progressdialog
if(upVoteProgressDialog != null){
upVoteProgressDialog.dismiss();
upVoteProgressDialog = null;
}
upVoteProgressDialog = new ProgressDialog(DealsDetailsActivity.this);
// Set progressdialog message
upVoteProgressDialog.setMessage("Saving...");
upVoteProgressDialog.setIndeterminate(false);
// Show progressdialog
upVoteProgressDialog.show();
}

@Override
protected Void doInBackground(Void... params) {
queryParse();

// if deal found correctly
if (deal != null) {
// if user has not voted yet
if (dealVoteUser == null) {
// create new and assign vote to 1
dealVoteUser = new ParseObject("deal_vote_users");
dealVoteUser.put("deal", deal);
dealVoteUser.put("user", ParseUser.getCurrentUser());
dealVoteUser.put("vote", 1);
up_votes = deal.getInt("up_votes") + 1;
down_votes = deal.getInt("down_votes");
// if user already down voted
} else if (dealVoteUser.get("vote").toString().equals("0")) {
// change vote to 1
dealVoteUser.put("vote", 1);
up_votes = deal.getInt("up_votes") + 1;
down_votes = deal.getInt("down_votes") - 1;
// if user already up voted
} else if (dealVoteUser.get("vote").toString().equals("1")) {
// already voted up, remove vote
dealVoteUser.put("vote", 2);
up_votes = deal.getInt("up_votes") - 1;
down_votes = deal.getInt("down_votes");
// if user already voted but cleared vote
} else if (dealVoteUser.get("vote").toString().equals("2")) {
// change vote to 1
dealVoteUser.put("vote", 1);
up_votes = deal.getInt("up_votes") + 1;
down_votes = deal.getInt("down_votes");
}

// calculate overall rating percentage
if ((up_votes + down_votes) != 0) {
rating = (up_votes / (up_votes + down_votes)) * 100;
} else if ((up_votes == 0) && (down_votes == 0)) {
rating = 0;
} else {
rating = 50;
}

deal.put("rating", rating);
deal.put("up_votes", up_votes);

try {
deal.save();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
dealVoteUser.save();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
// deal not found problem
}

return null;
}

@Override
protected void onPostExecute(Void result) {
// visually change buttons without querying db
setButtons(false);

//remove progress dialogue
if(upVoteProgressDialog != null){
upVoteProgressDialog.dismiss();
upVoteProgressDialog = null;
}
}
}

最佳答案

使用 saveInBackground 方法 - 它与 save 的作用相同,但也会将其保存到应用程序的缓存中,这样您就不会在获取数据时得到不同的值正在保存,因此它不会对您的应用程序产生任何明显影响。这是保存或查找的最佳方法(它有一个名为 findInBackground 的“姊妹”方法)。它就像一个异步任务,不会阻塞您的主线程。

关于android - parse.com 调用 .save() 导致所有查询停止运行 Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24637690/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com