Skip to content

runTransactionBlock works wrong in offline mode #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ab-rdarts opened this issue May 18, 2017 · 9 comments
Closed

runTransactionBlock works wrong in offline mode #18

ab-rdarts opened this issue May 18, 2017 · 9 comments

Comments

@ab-rdarts
Copy link

ab-rdarts commented May 18, 2017

[READ] Step 1: Are you in the right place?

Not sure

[REQUIRED] Step 2: Describe your environment

Xcode version: 8.3.2
Firebase SDK version: 4.0.0
Library version: 4.0.0
Firebase Product: database

[REQUIRED] Step 3: Describe the problem

Steps to reproduce:

initial count value in database is 0
application started and wifi connection is switched off
application performs two calls to runTransactionBlock which increases counter by 1. database value is still 0 as expected
it works fine locally
now wifi connection is switched on
runTransaction successfully committed two values, but database value equals to 1

EXPECTED: database value should be equal to 2
please note that if runTransactionBlock called in offline 7 times than server value is 6 instead of 7.

Relevant Code:

        self.ref.child("values/contributed").runTransactionBlock({ (data) -> TransactionResult in
            if let value = data.value as? Int {
                debugPrint("FB BEFORE \(data)")
                data.value = value + 1
                debugPrint("FB AFTER \(data)")
                return .success(withValue: data)
            } else {
                data.value = 1
                debugPrint("FB EMPTY \(data)")
                return .success(withValue: data)
            }
        }) { (error, commited, snapshot) in
            if let error = error {
                debugPrint("FB ERROR: \(error)")
            } else {
                debugPrint("FB COMMITTED \(snapshot)")
            }
        }

offline output:
"FB BEFORE FIRMutableData (top-most transaction) (null) 0"
"FB AFTER FIRMutableData (top-most transaction) (null) 1"
"FB BEFORE FIRMutableData (top-most transaction) (null) 1"
"FB AFTER FIRMutableData (top-most transaction) (null) 2"

online output:
"FB BEFORE FIRMutableData (top-most transaction) (null) 0"
"FB AFTER FIRMutableData (top-most transaction) (null) 1"
"FB COMMITTED Optional(Snap (contributed) 1)"
"FB COMMITTED Optional(Snap (contributed) 1)"

@ab-rdarts
Copy link
Author

ab-rdarts commented May 19, 2017

Any comments? By the way it seems the same behavior on FirebaseDatabase version 3.1.2.

@schmidt-sebastian
Copy link
Contributor

Can you tell us how you are running the transactions multiple times? Do you wait for the transactions to complete before running them again?

It would also help to get debug output from your app. As one of the first lines in your app, please set "Database.setLoggingEnabled(true)". You also need to pass -FIRDebugEnabled to your app to make the logs appear.

@ab-rdarts
Copy link
Author

Yes, application runs multiple transactions one after another. And no, application doesn't wait for transaction completion before running the next one because it's called only in online mode. Do you mean that it's not possible to use multiple runTransactionBlock in offline? I didn't see such limitations in the documentation but maybe I have missed that. I will try to send log a little bit later.

@schmidt-sebastian
Copy link
Contributor

You can use multiple transactions when offline, but it changes the way we run them and if there is a race on the same database location, some transactions can get cancelled. I will be able to tell what's going on from the logs.

@ab-rdarts
Copy link
Author

@schmidt-sebastian It's strange but I cannot reproduce it now, maybe it was my mistake somewhere. So the issue can be closed at least for now.

@morganchen12
Copy link
Contributor

Thanks for following up @ab-rdarts. If this issue resurfaces in the future, please reopen this ticket.

@ab-rdarts
Copy link
Author

It's strange but I have the same problem with transaction during online
I've tried to add four times 100 to /jars/zv8qmAJ1IKPEQZkOUb0HNz52dB13/values/contributed
As a result I see 300 instead of 400, though I see 4 successful commits

log.txt

@morganchen12 morganchen12 reopened this Feb 22, 2019
@ab-rdarts
Copy link
Author

@schmidt-sebastian any comments about attached log?

@schmidt-sebastian
Copy link
Contributor

Closing this due to inactivity. Please re-open if this is still an issue.

@firebase firebase locked and limited conversation to collaborators Jan 21, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants