![](/img/trans.png)
[英]How to check if a class in already exists in parse and create programatically
[英]How can i check if a value exists already in a Parse data class Android
我正在尋找一種方法,如何檢查Android中的Parse
數據類中是否已存在電話號碼。
例如,檢查電話號碼是否已存在,如果是,則返回true,如果不是false。
我用過這個:
query1.whereEqualTo("phone", "0644444444");
query1.findInBackground(new FindCallback<ParseObject>() {
它沒有多大幫助。
在查詢中使用getFirstInBackground(),然后只需檢查是否存在ParseException.OBJECT_NOT_FOUND異常。 如果有,那么該對象不存在,否則它就在那里! 使用getFirstInBackground比findInBackground更好,因為getFirstInBackground只檢查並返回1個對象,而findInBackground可能必須查詢MANY對象。
例
query1.whereEqualTo("phone", "0644444444");
query1.getFirstInBackground(new GetCallback<ParseObject>()
{
public void done(ParseObject object, ParseException e)
{
if(e == null)
{
//object exists
}
else
{
if(e.getCode() == ParseException.OBJECT_NOT_FOUND)
{
//object doesn't exist
}
else
{
//unknown error, debug
}
}
}
});
通常,這些Parse回調會向您傳遞ParseException,您可以檢查異常上的狀態代碼。
query1.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> objects, ParseException ex) {
if(ex != null) {
final int statusCode = ex.getCode();
if (statusCode == ParsseException.OBJECT_NOT_FOUND) {
// Object did not exist on the parse backend
}
}
else {
// No exception means the object exists
}
}
}
您可以從getCode
找到許多其他更具體的錯誤代碼, 這里是Parse文檔的完整列表 。 一些類型的數據具有特定代碼,例如在處理注冊/登錄時,存在針對EMAIL_NOT_FOUND
特定代碼。
你能嘗試這樣的事嗎:
ParseQuery<ParseObject> query = ParseQuery.getQuery(PARSE_CLASS_NAME);// put name of your Parse class here
query.whereEqualTo("phoneList", "0644444444");
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> phoneList, ParseException e) {
if (e == null) {
handleIsUserNumberFound(! phoneList.isEmpty())
} else {
Log.d("error while retrieving phone number", "Error: " + e.getMessage());
}
}
});
public class handleIsUserNumberFound(boolean isUserNumberFound){
//do whatever you need to with the value
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.