写入检索到的文档时,Firestore事务失败(重试次数)

时间:2019-06-04 15:55:37

标签: java google-cloud-firestore

我相信这是预期的行为,但是我找不到这个例子。

作为系统的登录用户,我可以创建公司。创建公司时,我希望被列为所有者和关注者。因为我想轻松查看自己拥有或关注的企业,所以我想将有关创建的企业的信息保存到我的用户个人资料中。

在交易中,我请求有关用户的当前信息,然后设置数据。我相信,由于我正在检索文档然后尝试对其进行更新,因此Firestore会引发错误,因为一旦执行写操作,事务就会认为其中有新内容并再次获取结果,从而重新启动事务...当然直到失败为止。如果我不首先阅读文档,一切都很好...但是我想确保我的用户是最新的。

因此,我是在做错什么,还是我需要在其他地方获取userData,然后在其他位置进行修改? 谢谢!

userID = user.getUserID();
final String newBusinessID = db.collection("businesses").document().getId();
Toast.makeText(CreateBusiness.this, "newBusinessID    " + newBusinessID, Toast.LENGTH_SHORT).show();
final DocumentReference userFollows = db.collection("users").document(userID).collection("following").document(newBusinessID);
final DocumentReference businessFollowers = db.collection("businesses").document(newBusinessID).collection("followers").document(userID);
final DocumentReference userOwnsRef = db.collection("users").document(userID).collection("owns").document(newBusinessID);
final DocumentReference businessOwnerRef = db.collection("businesses").document(newBusinessID).collection("owners").document(userID);
final DocumentReference currentUserRef = db.collection("users").document(userID);
final DocumentReference newBusinessRef = db.collection("businesses").document(newBusinessID);
business.setId(newBusinessID);

    db.runTransaction(new Transaction.Function<Void>() {

    @Nullable
    @Override
    public Void apply(@NonNull Transaction transaction) throws FirebaseFirestoreException {


    //get user data

    ---> ISSUE HERE //DocumentSnapshot userData = transaction.get(db.collection("users").document(currentUserRef);
    //if (userData.exists()) {
    //User freshUser = userData.toObject(User.class);
    HashMap<String, String> ownerInfo = new HashMap<>();
    ownerInfo.put("name", user.getName()); //I want to use freshUser here
    ownerInfo.put("userId", user.getUserID());

    HashMap<String, String> businessInfo = new HashMap<>();
    businessInfo.put("name", business.getName());
    businessInfo.put("businessID", newBusinessID);

    //String userNameId = freshUser.getUserID() + " " + freshUser.getName();

    //write business

    transaction.set(newBusinessRef, business);

    //write owner info to business
    transaction.set(businessOwnerRef, ownerInfo);

    //write business info to user

    transaction.set(userOwnsRef, businessInfo);

    //write info for following and followers

    transaction.set(userFollows, businessInfo);

    transaction.set(businessFollowers, ownerInfo);
                            //}
    return null;
    }
    }).addOnSuccessListener(new OnSuccessListener<Void>() {
        @Override
        public void onSuccess(Void aVoid) {
        Toast.makeText(CreateBusiness.this, "Business Added ", Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(CreateBusiness.this, BusinessProfile.class);
        intent.putExtra("BUSINESS", new Gson().toJson(business));
                            intent.putExtra("USER", new Gson().toJson(user));
                            startActivity(intent);
                        }
    }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
        Toast.makeText(CreateBusiness.this, e.toString(), Toast.LENGTH_SHORT).show();

        }
    });

Error message from inside App

0 个答案:

没有答案