[Python] Performing Collection Queries Within a Transaction: Python vs. Swift

Discussão em 'Python' iniciado por Stack, Março 10, 2025.

  1. Stack

    Stack Membro Participativo

    In Python, we can perform queries on collections within a transaction using the following code snippet:

    def write_upload_success(filename: str, uid: str, doc_id: str) -> DocumentReference:
    db = firestore.client()

    transaction = db.transaction()

    @firestore.transactional
    def transaction_operation(transaction) -> str:
    # Query to check if the filename exists
    notes_ref = db.collection('users').document(uid).collection('notes')

    # Find the largest order number in the existing notes
    order_query = notes_ref.order_by('order', direction=firestore.Query.DESCENDING).limit(1).get(transaction=transaction)

    max_order_note = None

    for note in order_query:
    max_order_note = note
    break

    order = max_order_note.get('order') + 1 if max_order_note else 1

    note_ref = notes_ref.document(doc_id)

    #
    # ...
    #

    return note_ref

    return transaction_operation(transaction)


    It appears that Python allows querying collections within a transaction using:

    notes_ref.order_by('order', direction=firestore.Query.DESCENDING).limit(1).get(transaction=transaction)


    However, I haven't been able to find an equivalent approach in Swift. Is it not possible to perform collection queries within a transaction in Swift?

    Thanks.

    p/s Almost all AIs will wrongly suggest using transaction.getDocuments in Swift, which this function doesn't exist.

    Continue reading...

Compartilhe esta Página