Search code examples
swiftfirebase-realtime-databasensmutabledata

Firebase swift runTransactionBlock : Cast from 'MutableData?' to unrelated type 'String' always fails


I need to run transactionBlock over a value, instead of a child.

Data structure:

-parent
    - child : value

When I try to cast the MutableData I'll get from child I have a crash. Xcode throws the warning confirming that:

 Cast from 'MutableData?' to unrelated type 'String' always fails

Snip of the code

Database.database().reference().child("parent").child("child").runTransactionBlock({ (currentData: MutableData!) -> TransactionResult in
                                        
    /**
     *
     * When runTransaction is called, it called straight away doTransaction with a supposed currentData that is the cached value and is often nil the first call.
     * Inside the doTransaction I have to make some actions based to the supposed actual data. Those actions have to work on the new value I want to set for the
     * currentData. Once that is done i send currentData gathered from currentDta.getDtata() which on the first call is just supposed and often nil and the
     * new value that i set with currentData.setData(). If the supposed current value and real current value are the same then I wite the data with the value set
     * in setData() , otherwise I do nothing and I go back with the real value I have in the database in that moment and I repeat.
     *
     *
     */
    
 
    let currentDataValue = currentData.value as! String

Below a screenshot as requested: enter image description here

From official Documentation this should be doable : https://firebase.google.com/docs/reference/ios/firebasedatabase/api/reference/Classes/FIRMutableData

How is the correct way to do that? All the examples found are with child [String:Any] I can't snapshot millions of records from parent just to check/write one child.

Thank you.


Solution

  • For anyone else who came across the same, see below

    enter image description here