找到值后如何停止在地图上发送信号

时间:2019-08-23 18:23:59

标签: java android google-cloud-firestore

我要执行的操作:当用户创建帖子时,让迭代器遍历文档以查看用户是否有太多帖子。如果用户的字段为空,则允许

enter image description here

我面临的问题是,当用户创建一个帖子时,它会遍历列表并在每个null值(而不仅仅是一个)上发布。

为解决此问题,我添加了break;。这解决了我的问题,但是由于某些原因,当使用break;语句时,迭代器将仅检查文档中的第5个项目是否为null,而不是所有文档。

代码:

    currentDocument.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DocumentSnapshot> task) {
        if (task.isSuccessful()) {
            DocumentSnapshot document = task.getResult();
            if (document.exists()) {
                Map<String, Object> map = document.getData();

                    for (Map.Entry<String, Object> entry : map.entrySet()) {

                        Log.d(TAG, map.entrySet().toString());
                        Log.d(TAG, entry.toString());

                        if (entry.getValue() == null) {

                            Log.d("tag", entry.toString() + " is equal to null");

                            posted....
                            break;

要解决此问题,我发现了这个stack post,他们说要添加一个布尔值标志(已删除break:)

public static boolean criteriaMet=false;

currentDocument.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
    if (task.isSuccessful()) {
        DocumentSnapshot document = task.getResult();
        if (document.exists()) {
            Map<String, Object> map = document.getData();

            while (criteriaMet == false) {

            for (Map.Entry<String, Object> entry : map.entrySet()) {

                Log.d(TAG, map.entrySet().toString());
                Log.d(TAG, entry.toString());

                if (entry.getValue() == null) {
                criteriaMet = true;

                    Log.d("tag", entry.toString() + " is equal to null");

                    posted....

当我使用此方法时,用户将其发布四次,并且(跳过第一个空值,由itterator标识并在剩余的4个上发布),如果用户尝试不发布任何内容,那么我不会认为该变量正在重置,但不确定

带有第二种方法的完整代码被注释掉了,我还有其他一些我正在创建/编写的文档是不相关的:

        newPostButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {


            currentDocument.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
                @Override
                public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                    if (task.isSuccessful()) {
                        DocumentSnapshot document = task.getResult();
                        if (document.exists()) {
                            Map<String, Object> map = document.getData();

                           //     while (criteriaMet == false) {

                                    for (Map.Entry<String, Object> entry : map.entrySet()) {

                                        Log.d(TAG, map.entrySet().toString());
                                        Log.d(TAG, entry.toString());

                                        if (entry.getValue() == null) {
                                            criteriaMet = true;

                                            Log.d("tag", entry.toString() + " is equal to null");

                                            postProgress.setTitle("Posting");
                                            postProgress.setMessage("This Should Only Take A Second");
                                            postProgress.setCanceledOnTouchOutside(false);
                                            postProgress.show();

                                            final String desc = newPostDesc.getText().toString();

                                            if (!TextUtils.isEmpty(desc)) {

                                                final String randomName = UUID.randomUUID().toString();

                                                final StorageReference filePath = storageReference.child("post_images").child(randomName + ".jpeg");
                                                filePath.putFile(postImageUri).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
                                                    @Override
                                                    public void onComplete(@NonNull final Task<UploadTask.TaskSnapshot> task) {

                                                        if (task.isSuccessful()) {

                                                            File newImageFile = new File(Objects.requireNonNull(postImageUri.getPath()));
                                                            try {
                                                                compressedImageFile = new Compressor(NewPost.this)
                                                                        .setMaxHeight(200)
                                                                        .setMaxWidth(200)
                                                                        .setQuality(10)
                                                                        .compressToBitmap(newImageFile);

                                                            } catch (IOException e) {
                                                                e.printStackTrace();
                                                            }

                                                            ByteArrayOutputStream baos = new ByteArrayOutputStream();
                                                            compressedImageFile.compress(Bitmap.CompressFormat.JPEG, 100, baos);
                                                            byte[] thumbData = baos.toByteArray();

                                                            final UploadTask uploadTask = storageReference.child("post_images/thumbs").child(randomName + ".jpeg").putBytes(thumbData);

                                                            uploadTask.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                                                                @Override
                                                                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

                                                                }
                                                            }).addOnFailureListener(new OnFailureListener() {
                                                                @Override
                                                                public void onFailure(@NonNull Exception e) {


                                                                }
                                                            });

                                                            filePath.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                                                                @Override
                                                                public void onSuccess(Uri uri) {

                                                                    final String downloadUrl = uri.toString();
                                                                    Log.d("tag", downloadUrl);

                                                                    FirebaseUser current_user = FirebaseAuth.getInstance().getCurrentUser();
                                                                    String uid = Objects.requireNonNull(current_user).getUid();

                                                                    final Map<String, Object> postMap = new HashMap<>();
                                                                    // No thumb ?????
                                                                    postMap.put("image_url", downloadUrl);
                                                                    postMap.put("desc", desc);
                                                                    postMap.put("user_id", current_user_id);
                                                                    postMap.put("message_doc", uid + postCategory);
                                                                    postMap.put("timestamp", FieldValue.serverTimestamp());


                                                                    firebaseFirestore.collection(postCategory).add(postMap).addOnCompleteListener(new OnCompleteListener<DocumentReference>() {
                                                                        @Override
                                                                        public void onComplete(@NonNull Task<DocumentReference> task) {

                                                                            if (task.isSuccessful()) {


                                                                                firebaseFirestore.collection("Posts").add(postMap).addOnCompleteListener(new OnCompleteListener<DocumentReference>() {
                                                                                    @Override
                                                                                    public void onComplete(@NonNull final Task<DocumentReference> task) {

                                                                                        FirebaseUser current_user = FirebaseAuth.getInstance().getCurrentUser();
                                                                                        String uid = Objects.requireNonNull(current_user).getUid();

                                                                                        final Map<String, String> chatMap = new HashMap<>();
                                                                                        postMap.put("timestamp", FieldValue.serverTimestamp());
                                                                                        postMap.put("name", current_user_id);
                                                                                        postMap.put("message", "");

                                                                                        firebaseFirestore.collection("Messages")
                                                                                                .document(uid + postCategory)
                                                                                                .set(chatMap)
                                                                                                .addOnSuccessListener(new OnSuccessListener<Void>() {
                                                                                                    @Override
                                                                                                    public void onSuccess(Void aVoid) {
                                                                                                        final String BlogPostID = Objects.requireNonNull(task.getResult()).getId();

                                                                                                        String document_Id = current_user_id + postCategory;

                                                                                                        final Map<String, String> usermap = new HashMap<>();
                                                                                                        usermap.put("Owner", current_user_id);
                                                                                                        usermap.put("debater", null);

                                                                                                        firebaseFirestore.collection("Messages").document(document_Id)
                                                                                                                .set(usermap)
                                                                                                                .addOnSuccessListener(new OnSuccessListener<Void>() {
                                                                                                                    @Override
                                                                                                                    public void onSuccess(Void aVoid) {

                                                                                                                        //write to active posts document

                                                                                                                        postProgress.dismiss();
                                                                                                                        Intent startIntent = new Intent(NewPost.this, MainActivity.class);
                                                                                                                        startActivity(startIntent);

                                                                                                                    }
                                                                                                                });
                                                                                                    }
                                                                                                });


                                                                                    }
                                                                                });

                                                                            }


                                                                        }
                                                                    });


                                                                }
                                                            });

                                                        }
                                                    }
                                                });


                                            }


                                        } else {
                                            Log.w(TAG, "the value is not null");

                                            Toast.makeText(getApplicationContext(), "Too many active debates", Toast.LENGTH_LONG).show();

                                        }

                                        break;
                                    }
                              //  } //attached to while crit loop
                        }

                    }
                }
            });
        }

    });}

我已经为此工作了几天,如果需要更多信息,我会感到困惑。

0 个答案:

没有答案