我想要的是过滤来自" uId"的用户,比如Sqlite
SELECT * FROM users WHERE uId IN ('OTky08PtlIYDE4KHRIzvZxyqIKp1' , 'UiHUd9BhpybDAmTiGTbgg30THPN2')
我们可以通过哪种方式直接在firebase数据库上获得这种过滤器?
答案 0 :(得分:2)
我有类似的问题,通过ID检索用户。我发现无法在IN
中进行Firebase
查询。
David在他的video中建议对数据库进行非规范化或对每个id进行查询。
我不喜欢非规范化,所以我查询了每个用户。 如果你进入RxJava,这就是我做的事情
@NonNull
private Observable<List<User>> userIdsToUsers(@NonNull final List<String> uids) {
if (uids.isEmpty()) {
return Observable.just(new ArrayList<>());
}
return Observable.combineLatest(userIdsToUserObservables(uids),
new ListCastFunc<>(User.class));
}
@NonNull
private List<Observable<User>> userIdsToUserObservables(@NonNull final List<String> uids) {
final List<Observable<User>> result = new ArrayList<>();
for (final String uid : uids) {
result.add(userIdToUserObservable(uid));
}
return result;
}
@NonNull
private Observable<User> userIdToUserObservable(@NonNull final String uid) {
return RxFirebaseDatabase.observeValueEvent(getUsersReference().orderByKey().equalTo(uid))
.flatMap(this::dataSnapshotToUserObservable);
}
@NonNull
private Observable<User> dataSnapshotToUserObservable(@NonNull final DataSnapshot ds) {
return Observable.fromCallable(() -> dataSnapshotToUser(ds));
}
@Nullable
private static User dataSnapshotToUser(@NonNull DataSnapshot ds) {
if (!ds.exists()) {
return null;
}
return parsedObjHere;
}
private static final class ListCastFunc<T> implements FuncN<List<T>> {
@NonNull
private final Class<T> targetClass;
ListCastFunc(@NonNull final Class<T> targetClass) {
this.targetClass = targetClass;
}
@Override
public List<T> call(final Object... args) {
if (args == null || args.length == 0) {
return new ArrayList<>();
}
final List<T> results = new ArrayList<>(args.length);
for (final Object arg : args) {
if (arg != null && targetClass.isAssignableFrom(arg.getClass())) {
//noinspection unchecked
results.add((T) arg);
}
}
return results;
}
}
答案 1 :(得分:0)
usersRef.orderByChild("uId").startAt("OTky08PtlIYDE4KHRIzvZxyqIKp1").endAt("UiHUd9BhpybDAmTiGTbgg30THPN2")