Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
980 views
in Technique[技术] by (71.8m points)

swift - Firebase Firestore - OR query alternative

In this question Firebase Firestore - OR query they said there is no OR query. This means I can't easily query for items that have an ID that is in an array I got earlier.

// What I want
documentRef
    .whereField("itemId", isEqualTo: "id1")
    .orWhereField("itemId", isEqualTo: "id3")

// or
documentRef
    .whereField("itemId", in: idArray)

Since this doesn't work, what option is better to query my data?


  1. Get all the data and check locally

I could get all my records and just check the IDs on the device but the problem is that it can be up to 1000 records and the user might only need three of them.


  1. Do separate queries for all items

I could just do a query whereField("itemId", isEqualTo: "id") for each item that the user needs. This would be good if the user only needs a few of the items but it is possible the user needs all the items and this solution would become a lot slower than the first one. (unless you could batch reads)


  1. Move data

I think I could move the purchased data to the Item collection: Items/itemid/PurchasedBy/uid but I want to retrieve the Item that a user has bought and I don't know how to query an Item by a field inside a nested collection. I also don't know if this would be the correct place for the data.


My Data:

Users/uid/PurchasedItems/itemid

    payedInMoney: "$0.00"
    payedInCoins: "100"
    itemId: "itemid"
    timeStamp: timestamp

Items/itemid

    itemId: "itemid"
    // Other item info

Edit:

I made the first option because that is how I did it on RTDB but even with persistence on and using addSnapshotListener Firestore doesn't even come close to the speed of the RTDB implementation. I clocked the times and it looks like Firestore is at least 4x as slow as RTDB (both with persistence enabled).

// iPad 5 (2017) - iOS 11
RTDB:  50-100ms
FST:  350-450ms

// iPad Air (2014) - iOS 10
RTDB: 100-150ms
FST:  700-850ms

// iPad Mini (2012) - iOS 9
RTDB: 150-200ms
FST:  2900-3200ms

Edit 2:

Decided to stick with RTDB until persistence on Firestore really works.

Edit 3:

The accepted answer fixes the problem in this question and even though it doesn't fix all mine, it might help someone else.

After tests using only one query with Firestore the result still takes longer to come through than doing the nested query in RTDB so I will stick with my decision for now.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...