gpt4 book ai didi

java - Android:如何将查询与来自 Parse.com 的 Bolt 同步?

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:01:23 25 4
gpt4 key购买 nike

我正在使用 Parse.com 作为我的应用程序的后端。他们还提供本地数据库来存储信息,作为 SQLite 的替代方案。

我想通过解析将电话中的号码添加到我的数据库中。在添加数字之前,我需要检查该数字是否已存在于数据库中,因此我使用 findInBackground() 获取与我要添加的数字相匹配的数字列表。如果列表为空,则数据库中不存在我要添加的号码。

实现的方法是:

public void putPerson(final String name, final String phoneNumber, final boolean isFav) {

// Verify if there is any person with the same phone number
ParseQuery<ParseObject> query = ParseQuery.getQuery(ParseClass.PERSON_CLASS);
query.whereEqualTo(ParseKey.PERSON_PHONE_NUMBER_KEY, phoneNumber);
query.fromLocalDatastore();
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> personList,
ParseException e) {
if (e == null) {
if (personList.isEmpty()) {
// If there is not any person with the same phone number add person
ParseObject person = new ParseObject(ParseClass.PERSON_CLASS);
person.put(ParseKey.PERSON_NAME_KEY, name);
person.put(ParseKey.PERSON_PHONE_NUMBER_KEY, phoneNumber);
person.put(ParseKey.PERSON_FAVORITE_KEY, isFav);
person.pinInBackground();

Log.d(TAG,"Person:"+phoneNumber+" was added.");
} else {
Log.d(TAG, "Warning: " + "Person with the number " + phoneNumber + " already exists.");
}
} else {
Log.d(TAG, "Error: " + e.getMessage());
}
}
}
);
}

然后我调用此方法 3 次以添加 3 个数字:

ParseLocalDataStore.getInstance().putPerson("Jack", "0741234567", false);
ParseLocalDataStore.getInstance().putPerson("John", "0747654321", false);
ParseLocalDataStore.getInstance().putPerson("Jack", "0741234567", false);
ParseLocalDataStore.getInstance().getPerson(); // Get all persons from database

请注意,第三个数字与第一个相同,不应将其添加到数据库中。但是 logcat 显示:

12-26 15:37:55.424 16408-16408/D/MGParseLocalDataStore: Person:0741234567 was added.
12-26 15:37:55.424 16408-16408/D/MGParseLocalDataStore: Person:0747654321 was added.
12-26 15:37:55.484 16408-16408/D/MGParseLocalDataStore: Person:0741234567 was added.

第三个数字即使不应该这样做也添加了,因为fintInBackground()几乎同时在3个后台线程中运行,所以它会发现没有数字在类似于我要添加的数据库。

this 问题中,有人告诉我应该使用 Parse 中的 Bolts 库。我从 here 和一些 Parse 博客文章中了解到它,但我不完全理解如何使用我已有的方法使用它,以及如何同步要一个接一个地执行的查询。

如果有人使用过此库,请指导我如何操作或提供一些基本示例,以便我了解工作流程。

谢谢!

最佳答案

听起来你有竞争条件。有很多不同的方法可以解决这个问题。这是一个非 bolt 替代方案。

主要问题是搜索查询大致在同一时间发生,因此它们不会在数据库中找到重复项。在这种情况下,我们解决它的方法是一次只搜索和添加一个人,显然不在主线程上,因为我们不想占用 ui 进行搜索。因此,让我们创建一个列表来做两件事:1) 包含需要添加的项目,2) 验证它们是否可以添加。我们可以使用异步任务让我们的搜索远离主线程。

以下是需要完成的粗略想法:

import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;

import java.util.ArrayList;
import java.util.List;

public class AddPersonAsyncQueue {

ArrayList<ParseObject> mPeople = new ArrayList();
Boolean mLock;
AddTask mRunningTask;

public synchronized void addPerson(final String name, final String phoneNumber, final boolean isFav) {

// we aren't adding a person just yet simply creating the object
// and keeping track that we should do the search then add for this person if they aren't found
ParseObject person = new ParseObject(ParseClass.PERSON_CLASS);
person.put(ParseKey.PERSON_NAME_KEY, name);
person.put(ParseKey.PERSON_PHONE_NUMBER_KEY, phoneNumber);
person.put(ParseKey.PERSON_FAVORITE_KEY, isFav);

synchronized (mLock) {
mPeople.add(person);
}
processQueue();
}

public boolean processQueue() {
boolean running = false;

synchronized (mLock) {
if (mRunningTask == null) {
if (mPeople.size() > 0) {

mRunningTask = new AddTask(null);
mRunningTask.execute();
running = true;
} else {
// queue is empty no need waste starting an async task
running = false;
}
} else {
// async task is already running since it isn't null
running = false;
}
}
return running;
}

protected void onProcessQueueCompleted() {
mRunningTask = null;
}

private class AddTask extends AsyncTask<Void, ParseObject, Boolean> {
AddPersonAsyncQueue mAddPersonAsyncQueue;

public AddTask(AddPersonAsyncQueue queue) {
mAddPersonAsyncQueue = queue;
}

@Override
protected Boolean doInBackground(Void... voids) {
boolean errors = false;
ParseObject nextObject = null;

while (!isCancelled()) {

synchronized (mLock) {
if (mPeople.size() == 0) {
break;
} else {
// always take the oldest item fifo
nextObject = mPeople.remove(0);
}
}

if (alreadyHasPhoneNumber(nextObject.getInt(ParseKey.PERSON_PHONE_NUMBER_KEY))) {
// do nothing as we don't want to add a duplicate
errors = true;
} else if (addPerson(nextObject)) {
// nice we were able to add successfully

} else {
// we weren't able to add the person object we had an error
errors = true;
}

}
return errors;
}

private boolean alreadyHasPhoneNumber(int phoneNumber) {
try {
ParseQuery<ParseObject> query = ParseQuery.getQuery(ParseClass.PERSON_CLASS);
query.whereEqualTo(ParseKey.PERSON_PHONE_NUMBER_KEY, phoneNumber);
query.fromLocalDatastore();
List<ParseObject> objects = query.find();
return objects.size() > 0;
} catch (Exception error) {
// may need different logic here to do in the even of an error
return true;
}
}

private boolean addPerson(ParseObject person) {
try {
// now we finally add the person
person.pin();
return true;
} catch (ParseException e) {
e.printStackTrace();
return false;
}
}

@Override
protected void onPostExecute(Boolean aBoolean) {
super.onPostExecute(aBoolean);
onProcessQueueCompleted();
}
}


}

关于java - Android:如何将查询与来自 Parse.com 的 Bolt 同步?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34492689/

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