删除存储在 ParseObject 数组列表中的 String 的单个实例
Remove a single instance of a String stored in a ParseObject Array List
我正在尝试从 Parse 中的数组字段中删除字符串的单个实例:
final ParseObject post = mPosts.get(position);
List<String> repliedToByList = post.getList("repliedToBy"); // Retrieve current list
ParseUser currentUser = ParseUser.getCurrentUser().getObjectId();
repliedToByList.remove(currentUserObjectId); // Remove first instance of specified objectid
post.remove("repliedToBy"); // Clear the entire list
post.addAllUnique("repliedToBy", Collections.singletonList(repliedToByList)); // Add the new list
该字符串是当前用户的objectId,当用户回复"post"时添加,如:
post.addAll("repliedToBy", Collections.singletonList(ParseUser.getCurrentUser().getObjectId()));
例如,数组包含:
["1RtqEgy1ct","f4qEWY8UOM","f4qEWY8UOM"]
有什么方法可以只删除一个 f4qEWY8UOM
实例吗?替代方案是使用 increments/decrements 完成所有操作,但这对我来说并不理想。
增加我的回复数很容易:
// Increment replyCount
postObject.increment("replyCount", 1);
postObject.saveInBackground();
但是 Android Parse SDK 声明的递减方法有问题,即 postObject.increment("replyCount", -1)
,所以我尝试了以下方法:
我不是将 objectId 列表存储在列表中然后计算其大小,而是简单地 increment/decrement 通过以下方式,始终获取先前的 replyCount,减去 1,然后保存新的 put 调用:
// Decrement replyCount
int newReplyCount = postObject.getInt("replyCount") - 1;
postObject.put("replyCount", newReplyCount);
postObject.saveInBackground();
我正在尝试从 Parse 中的数组字段中删除字符串的单个实例:
final ParseObject post = mPosts.get(position);
List<String> repliedToByList = post.getList("repliedToBy"); // Retrieve current list
ParseUser currentUser = ParseUser.getCurrentUser().getObjectId();
repliedToByList.remove(currentUserObjectId); // Remove first instance of specified objectid
post.remove("repliedToBy"); // Clear the entire list
post.addAllUnique("repliedToBy", Collections.singletonList(repliedToByList)); // Add the new list
该字符串是当前用户的objectId,当用户回复"post"时添加,如:
post.addAll("repliedToBy", Collections.singletonList(ParseUser.getCurrentUser().getObjectId()));
例如,数组包含:
["1RtqEgy1ct","f4qEWY8UOM","f4qEWY8UOM"]
有什么方法可以只删除一个 f4qEWY8UOM
实例吗?替代方案是使用 increments/decrements 完成所有操作,但这对我来说并不理想。
增加我的回复数很容易:
// Increment replyCount
postObject.increment("replyCount", 1);
postObject.saveInBackground();
但是 Android Parse SDK 声明的递减方法有问题,即 postObject.increment("replyCount", -1)
,所以我尝试了以下方法:
我不是将 objectId 列表存储在列表中然后计算其大小,而是简单地 increment/decrement 通过以下方式,始终获取先前的 replyCount,减去 1,然后保存新的 put 调用:
// Decrement replyCount
int newReplyCount = postObject.getInt("replyCount") - 1;
postObject.put("replyCount", newReplyCount);
postObject.saveInBackground();