LoginSignup
1
0

More than 3 years have passed since last update.

cloudfirestore 複数ドキュメント 取得でnot equalを使う方法

Posted at

not equalがない

firestoreにはnot equalがないようです。
whereでlessthanとgraterthanを組み合わせればいけると書いてあったのですが
文字列の場合だといけなかった。。。のでfilterを使って対処

struct UserData{
    let uuid:String
    let userName:String
    let nowLatitude: Double
    let nowLongitude: Double
    let updatedAt: Timestamp

    init(uuid:String,userName:String,nowLatitude: Double, nowLongitude: Double, updatedAt: Timestamp) {
        self.uuid = uuid
        self.userName = userName
        self.nowLatitude = nowLatitude
        self.nowLongitude = nowLongitude
        self.updatedAt = updatedAt
    }

    init(document: [String: Any]) {
        uuid = document["uuid"] as? String ?? ""
        userName = document["user_name"] as? String ?? ""
        nowLatitude = document["now_latitude"] as? Double ?? 0
        nowLongitude = document["now_longitude"] as? Double ?? 0
        updatedAt = document["updated_at"] as? Timestamp ?? Timestamp()
    }
}

てなかんじで辞書を作っていれば
以下のようにフィルターをかけてあげればできる


db.collection("users")
            .addSnapshotListener { querySnapshot, error in
            guard let documents = querySnapshot?.documents else {
                print("Error fetching documents: \(error!)")
                return
            }
            print("userDateReload")
            self.usersList = documents
                .filter{UserData(document:$0.data()).uuid != self.AppDelegateDatas.UUID }
                .map { UserData(document: $0.data()) }
                print(self.usersList)
        }

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0