我需要在parse.com中批量保存一些对象,而且效果很好,但是我无法使用传统方式获得对象IDS 。我正在使用 saveallinbackroung ,通过 ArrayList parseObjects 。数据保存并获得回调成功,但是我的parseObject在阵列列表中没有获得任何额外的数据,包括Objectid。
objectsToUpload = new ArrayList<ParseObject>();
cursor.moveToFirst();
int titleIndex = cursor.getColumnIndex(Items.TITULO);
int subtitleIndex = cursor.getColumnIndex(Items.SUBTITULO);
int syncedIndex = cursor.getColumnIndex(Items.SYNCED);
int dateIndex = cursor.getColumnIndex(Items.DATE);
int entryIdIndex = cursor.getColumnIndex(Items.ENTRY_ID);
int idIndex = cursor.getColumnIndex(Items._ID);
while (!cursor.isAfterLast()) {
String title = cursor.getString(titleIndex);
String subtitle = cursor.getString(subtitleIndex);
String date = cursor.getString(dateIndex);
int syncedInt = cursor.getInt(syncedIndex);
ParseObject object = new ParseObject(Constants.PARSE_OBJ_NAME);
object.put(Items._ID, idIndex);
object.put(Items.TITULO, title);
object.put(Items.SUBTITULO, subtitle);
object.put(Items.SYNCED, true);
object.put(Items.DATE, date);
objectsToUpload.add(object);
cursor.moveToNext();
}
ParseObject.saveAllInBackground(objectsToUpload, new SaveCallback() {
@Override
public void done(ParseException e) {
if ( e == null )
{
for ( int i =0; i < objectsToUpload.size(); i++ )
{
ParseObject obectUploaded = objectsToUpload.get( i );
String objId = objectsToUpload.get( i ).getObjectId().toString();
}
}
}
});
字符串objid返回null。在调试中,对象库普洛德内部的parseobjects除了我之前提供的信息之外,没有任何内容。
我找不到Android的解析SDK中的适当解决方案,尽管我设法使用凌空图书馆使用 RESTAPI 解决了问题。它工作正常,我可以恢复保存对象的ID。
我推荐凌空,因为这是一种非常简单的方式,可以为您完成所有背景工作发送和接收JSONOBJECTS。掌握它可能有些棘手,但是您可以研究本教程。
我的解决方案的小片段
final Cursor cursor = fetchUnSynced();
if (cursor != null) {
cursor.moveToFirst();
int titleIndex = cursor.getColumnIndex(SyncAdapterContract.Items.TITULO);
int idIndex = cursor.getColumnIndex(SyncAdapterContract.Items._ID);
JSONObject dataToUpload = new JSONObject(); // The complete data to upload
JSONArray requestsArray = new JSONArray(); // Objects list
//Strings defined at parse.com
final String url = "https://api.parse.com/1/batch";
final String REQUESTS = "requests";
final String METHOD = "method";
final String PATH = "path";
final String BODY = "body";
final String pathUrl = "/1/classes/VolleyTest"; //Change to suit you needs
final String req_tag = "parse_batch";
//Iterate
while (!cursor.isAfterLast()) {
String title = cursor.getString(titleIndex);
JSONObject requestObj = new JSONObject(); //Request for each object
JSONObject bodyObjs = new JSONObject(); //Data of object
try {
//Body
bodyObjs.put(SyncAdapterContract.Items.TITULO, title);
//whatever else you have to upload at each object
//Request
requestObj.put(METHOD, "POST");
requestObj.put(PATH, pathUrl);
requestObj.put(BODY, bodyObjs);
requestsArray.put( requestObj );
} catch (JSONException e) {
e.printStackTrace();
}
cursor.moveToNext();
}
try {
dataToUpload.put(REQUESTS, requestsArray);
} catch (JSONException e) {
e.printStackTrace();
}
JsonArrayRequest jsonArrayReq = new JsonArrayRequest(
Request.Method.POST, url, dataToUpload,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
//Response with the ids and status of each object
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//Error
}
}
) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put( "X-Parse-REST-API-Key", Constants.PARSE_API_KEY );
headers.put( "X-Parse-Application-Id", Constants.PARSE_APP_ID );
return headers;
}
};
VolleySingleton.getInstance(MyApp.getsInstance()).addToRequestQueue(jsonArrayReq, req_tag );
要考虑的事情:
- 批处理操作仅支持每个呼叫的50个对象
- 齐射的正确实现应考虑 singleton class 与队列打交道