Firebase Runtransactionblock

firebase runTransactionBlock

Thanks to the comments seen above I was able to get this to work

....runTransactionBlock { (currentData: FIRMutableData) -> FIRTransactionResult in

var value = currentData.value as? Int

if value == nil {
value = 0
}

currentData.value = value! + 1
return FIRTransactionResult.successWithValue(currentData)

}

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

For anyone else who came across the same, see below

Sample Image

If firebase .runTransactionBlock completes with error, will it re-run itself?

Firebase Realtime Database automatically retries transactions in the case of contention/conflicting writes to/under the same path.

Only if the number of retries is exhausted will it call your completion handler, with an error indicating this problem. If you want to further retry the transaction at that point, you'll have to do so in your application code.

Firebase ,Swift: `runTransactionBlock()` returns null

As @Frank said in comments it's expected behaviour of runTransactionBloack to return NSNull initially , But if there already is a value in that location, it will get triggered again.And if there is a conflict in updating the value, It will trigger again.

Code:-

func updateTotalNoOfPost(completionBlock : (() -> Void)){

let prntRef = FIRDatabase.database().reference().child("TotalPosts")

prntRef.child("noOfTotalPost").runTransactionBlock({ (noOfPosts) -> FIRTransactionResult in
if let totalPost = noOfPosts.value as? Int{

noOfPosts.value = totalPost + 1
return FIRTransactionResult.successWithValue(noOfPosts)
}else{

return FIRTransactionResult.successWithValue(noOfPosts)

}
}, andCompletionBlock: {(error,completion,snap) in

print(error?.localizedDescription)
print(completion)
print(snap)
if !completion {

print("The value wasn't able to Update")
}else{

completionBlock()
}
})

}

For underlying concept:-

Data in transaction is null

Firebase runTransactionBlock() in iOS share extension

Like the answer you linked to stated, the NSXPCConnection error doesn't matter here.

The issue is that .runTransactionBlock() is asynchronous and .completeRequestReturningItems() will get called and exit the extension before you ever get a value from your Firebase database.

Try running .completeRequestReturningItems() in the andCompletionBlock.

override func didSelectPost() {
if self.sharedURL != nil {
// Send data to Firebase
self.myRootRef.runTransactionBlock({
(currentData:FMutableData!) in
var value = currentData.value as? String
// Getting the current value
// and checking whether it's null
if value == nil {
value = ""
}
// Setting the new value to the clipboard
// content
currentData.value = self.sharedURL?.absoluteString

// Finalizing the transaction
return FTransactionResult.successWithValue(currentData)
}, andCompletionBlock: {
// Completion Check
(error:NSError!, success:Bool, data:FDataSnapshot!) in
self.extensionContext!.completeRequestReturningItems([], completionHandler: nil)
}
)
}

}

Firebase runTransactionBlock() deletes node

If you set nil to a path in a Firebase database, it will do the same as a .removeValue().

When running a transaction you should check for nil. Since transactions can be called with nil if no default value was written.

myRootRef.runTransactionBlock({
(currentData:FMutableData!) in
var value = currentData.value as? Int
if value == nil {
value = 0
}
currentData.value = value! + 1
return FTransactionResult.successWithValue(currentData)
})

Read the Firebase Docs for more information.



Related Topics



Leave a reply



Submit