如果我使用查询在“ Android”中有用户ID arraylist,如何从“ Firestore”集合中获取文档的特定列表

时间:2019-03-20 15:23:08

标签: java android firebase google-cloud-firestore

实际上,我是将地图对象存储在我的Firestore文档下,其中包含一些我这样阻止的用户ID。click link below to view image

我有我在社交应用程序中阻止的所有成员ID,我需要使用Firestore查询来获取所有这些成员文档,但是如果arraylist具有多个索引,则查询将不起作用。但是当我只有一个ID时,该查询可以完美地工作。这是代码

 private ArrayList<String> blocks =new ArrayList<>();
    if (logedInMember != null){
        blocks = logedInMember.getBlocks();
        if (!blocks.isEmpty()){
            Query query;
            CollectionReference collection = firestore.collection(Constants.MEMBERS);
            query  = collection;

            for (int i = 0 ; i< blocks.size(); i++){
                Log.d("block member ID :", blocks.get(i) );
                // now its time to query all these ids
                String id = blocks.get(i);
                if (!id.isEmpty()){
                    query = query.whereEqualTo(Constants.ID,id);
                }
            }

            query.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                @Override
                public void onComplete(@NonNull Task<QuerySnapshot> task) {
                    if (task.isSuccessful()){


                        for (QueryDocumentSnapshot document : task.getResult()) {
                            Member member = document.toObject(Member.class);
                            Log.d("Member Id :", member.getId());
                            Log.d("Member Name :", member.getName());

                        }
                    }else {
                       // loader.dismissProgress();
                        Log.d("error : ","fail to load query");
                    }
                }
            });
}
}

1 个答案:

答案 0 :(得分:0)

  如果arraylist有多个索引,则

查询不起作用。但是当我   查询只有一个id完美

尝试

struct SomeDetails: Codable {
    let count: Int
    let name: String
    let type: String
}


struct SomeObject: Codable {
    let id: Int
    let details: SomeDetails

    enum CodingKeys: String, CodingKey {
        case id
    }

    enum DetailKeys: String, CodingKey {
        case count, name, type
    }

    init(from decoder: Decoder) throws {
        let topLevelContainer = try decoder.container(keyedBy: CodingKeys.self)
        let detailContainer = try decoder.container(keyedBy: DetailKeys.self)

        id = try topLevelContainer.decode(Int.self, forKey: .id)
        details = SomeDetails(
            count: try detailContainer.decode(Int.self, forKey: .count),
            name: try detailContainer.decode(String.self, forKey: .name),
            type: try detailContainer.decode(String.self, forKey: .type))
    }

    func encode(to encoder: Encoder) throws {
        var topLevelContainer = encoder.container(keyedBy: CodingKeys.self)
        try topLevelContainer.encode(id, forKey: .id)

        var detailContainer = encoder.container(keyedBy: DetailKeys.self)
        try detailContainer.encode(details.count, forKey: .count)
        try detailContainer.encode(details.name, forKey: .name)
        try detailContainer.encode(details.type, forKey: .type)
    }
}