我有非常简单的交易代码:
postRef.runTransaction(new Transaction.Handler() {
@Override
public Transaction.Result doTransaction(MutableData mutableData) {
Post post = mutableData.getValue(Post.class);
if (post == null) {
return Transaction.success(mutableData);
}
if(null == post.getLikes())
post.setLikes(new ArrayList<String>());
String currentUserId = // get id
if (post.getLikes().contains(currentUserId)) {
// Unstar the post and remove self from stars
post.setLikeCounts(post.getLikeCounts() - 1);
post.getLikes().remove(currentUserId);
} else {
// Star the post and add self to stars
post.setLikeCounts(post.getLikeCounts() + 1);
post.getLikes().add(currentUserId);
}
// Set value and report transaction success
mutableData.setValue(post);
return Transaction.success(mutableData);
}
@Override
public void onComplete(DatabaseError databaseError, boolean b,
DataSnapshot dataSnapshot) {
// Transaction completed
Log.d(LOG_TAG, "postTransaction:onComplete:" + databaseError);
}
});
但是我看到在likes
列表中添加字符串的奇怪行为根本没有修改列表:
post.getLikes().add(currentUserId);
我甚至停止了调试器并试图将硬编码值添加到列表中但仍然没有变化。
更新(发布课程):
package com.dsharew.aradavibes.model;
import com.dsharew.aradavibes.providers.LocalDataProvider;
import com.google.firebase.database.Exclude;
import java.util.ArrayList;
import java.util.List;
/**
* Created by dsharew on 6/1/17.
*/
public class Post extends Entity{
public static final String FIELD_AUTHOR_ID = "authorId";
public static final String FIELD_TITLE = "title";
public static final String FIELD_DESCRIPTION = "description";
public static final String FIELD_POST_IMAGE_IDS = "postImageIds";
public static final String FIELD_POST_VIDEO_IDS = "postVideoIds";
User author;
String title;
String description;
String authorId;
List<String> postImageIds;
List<String> postVideoIds;
List<PostImage> postImages;
List<PostVideo> postVideos;
int likeCounts;
List<String> likes;
int viewCounts;
List<String>views;
public Post(){
}
public Post(String title, String description, List<String> postImageIds, List<String> postVideoIds){
super();
this.title = title;
this.description = description;
this.postImageIds = postImageIds;
this.postVideoIds = postVideoIds;
}
public List<String> getPostImageIds() {
return postImageIds;
}
public void setPostImageIds(List<String> postImageIds) {
this.postImageIds = postImageIds;
}
public List<String> getPostVideoIds() {
return postVideoIds;
}
public void setPostVideoIds(List<String> postVideoIds) {
this.postVideoIds = postVideoIds;
}
@Exclude
public User getAuthor() {
return author;
}
public void setAuthor(User author) {
this.author = author;
}
public String getAuthorId() {
return authorId;
}
public void setAuthorId(String authorId) {
this.authorId = authorId;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Exclude
public List<PostImage> getPostImages() {
return postImages;
}
public void setPostImages(List<PostImage> postImages) {
this.postImages = postImages;
}
public int getLikeCounts() {
return likeCounts;
}
public void setLikeCounts(int likeCounts) {
this.likeCounts = likeCounts;
}
public List<String> getLikes() {
if(null == likes) return new ArrayList<String>();
return likes;
}
public void setLikes(List<String> likes) {
this.likes = likes;
}
@Exclude
public List<PostVideo> getPostVideos() {
if(null == postVideos)
postVideos = new ArrayList<PostVideo>();
return postVideos;
}
public int getViewCounts() {
return viewCounts;
}
public void setViewCounts(int viewCounts) {
this.viewCounts = viewCounts;
}
public List<String> getViews() {
return views;
}
public void setViews(List<String> views) {
this.views = views;
}
public void setPostVideos(List<PostVideo> postVideos) {
this.postVideos = postVideos;
}
public boolean validate(){
if(isEmpty()){
return false;
}
return true;
}
@Exclude
public boolean isEmpty(){
if( (null == getTitle() || getTitle().isEmpty()) &&
(null == getDescription() || getDescription().isEmpty()) &&
(null == getPostImages() || getPostImages().isEmpty()) &&
(null == getPostVideos() || getPostVideos().isEmpty())){
return true;
}
return false;
}
public void incrementViews(String currentUserId, String postId){
this.setViewCounts(this.getViewCounts() + 1);
this.getViews().add(currentUserId);
LocalDataProvider.getInstance().viewPost(postId);
}
public boolean containsUnsavedObjects(){
if(null != getPostImages() && !getPostImages().isEmpty()){
for(PostImage postImage : getPostImages()){
if(null != postImage && null == postImage.getId()) return true;
}
}
if(null != getPostVideos() && !getPostVideos().isEmpty()){
for(PostVideo postVideo : getPostVideos()){
if(null == postVideo.getId()) return true;
}
}
return false;
}
}
这里有任何线索吗?
答案 0 :(得分:1)
问题似乎是这种方法:
public List<String> getLikes() {
if(null == likes) return new ArrayList<String>();
return likes;
}
当likes
为空时,这将返回匿名ArrayList
。对该列表的任何更新都将丢失,即它们不会应用于Post
实例的状态。你可能打算:
public List<String> getLikes() {
if(null == likes) likes = new ArrayList<String>();
return likes;
}
在您的调试过程中,您可能怀疑getLikes()
有些奇怪的事情,并尝试用此修复它:
if(null == post.getLikes())
post.setLikes(new ArrayList<String>());
但这是无效的,因为getLikes()
永远不会返回null。