本文整理汇总了Java中com.google.firebase.database.Transaction类的典型用法代码示例。如果您正苦于以下问题:Java Transaction类的具体用法?Java Transaction怎么用?Java Transaction使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Transaction类属于com.google.firebase.database包,在下文中一共展示了Transaction类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: TransactionData
import com.google.firebase.database.Transaction; //导入依赖的package包/类
private TransactionData(
Path path,
Transaction.Handler handler,
ValueEventListener outstandingListener,
TransactionStatus status,
boolean applyLocally,
long order) {
this.path = path;
this.handler = handler;
this.outstandingListener = outstandingListener;
this.status = status;
this.retryCount = 0;
this.applyLocally = applyLocally;
this.order = order;
this.abortReason = null;
this.currentInputSnapshot = null;
this.currentOutputSnapshotRaw = null;
this.currentOutputSnapshotResolved = null;
}
示例2: runPostViewsCountTransition
import com.google.firebase.database.Transaction; //导入依赖的package包/类
public static void runPostViewsCountTransition(DatabaseReference postRef) {
postRef.runTransaction(new Transaction.Handler() {
@Override
public Transaction.Result doTransaction(MutableData mutableData) {
Post post = mutableData.getValue(Post.class);
if (post == null) {
return Transaction.success(mutableData);
}
int viewsCount = post.getViews() + 1;
post.setViews(viewsCount);
mutableData.setValue(post);
return Transaction.success(mutableData);
}
@Override
public void onComplete(DatabaseError databaseError, boolean b, DataSnapshot dataSnapshot) {
showLog("runCommentsCountTransition " + (databaseError != null));
/* if (databaseError == null) {
errorListener.onError();
}*/
}
});
}
示例3: updateProfileLikeCountAfterRemovingPost
import com.google.firebase.database.Transaction; //导入依赖的package包/类
public void updateProfileLikeCountAfterRemovingPost(Post post) {
DatabaseReference profileRef = database.getReference("profiles/" + post.getAuthorId() + "/likesCount");
final long likesByPostCount = post.getLikesCount();
profileRef.runTransaction(new Transaction.Handler() {
@Override
public Transaction.Result doTransaction(MutableData mutableData) {
Integer currentValue = mutableData.getValue(Integer.class);
if (currentValue != null && currentValue >= likesByPostCount) {
mutableData.setValue(currentValue - likesByPostCount);
}
return Transaction.success(mutableData);
}
@Override
public void onComplete(DatabaseError databaseError, boolean b, DataSnapshot dataSnapshot) {
LogUtil.logInfo(TAG, "Updating likes count transaction is completed.");
}
});
}
示例4: decrementCommentsCount
import com.google.firebase.database.Transaction; //导入依赖的package包/类
public void decrementCommentsCount(String postId, final OnTaskCompleteListener onTaskCompleteListener) {
DatabaseReference postRef = database.getReference("posts/" + postId + "/commentsCount");
postRef.runTransaction(new Transaction.Handler() {
@Override
public Transaction.Result doTransaction(MutableData mutableData) {
Integer currentValue = mutableData.getValue(Integer.class);
if (currentValue != null && currentValue >= 1) {
mutableData.setValue(currentValue - 1);
}
return Transaction.success(mutableData);
}
@Override
public void onComplete(DatabaseError databaseError, boolean b, DataSnapshot dataSnapshot) {
LogUtil.logInfo(TAG, "Updating comments count transaction is completed.");
if (onTaskCompleteListener != null) {
onTaskCompleteListener.onTaskComplete(true);
}
}
});
}
示例5: incrementWatchersCount
import com.google.firebase.database.Transaction; //导入依赖的package包/类
public void incrementWatchersCount(String postId) {
DatabaseReference postRef = database.getReference("posts/" + postId + "/watchersCount");
postRef.runTransaction(new Transaction.Handler() {
@Override
public Transaction.Result doTransaction(MutableData mutableData) {
Integer currentValue = mutableData.getValue(Integer.class);
if (currentValue == null) {
mutableData.setValue(1);
} else {
mutableData.setValue(currentValue + 1);
}
return Transaction.success(mutableData);
}
@Override
public void onComplete(DatabaseError databaseError, boolean b, DataSnapshot dataSnapshot) {
LogUtil.logInfo(TAG, "Updating Watchers count transaction is completed.");
}
});
}
示例6: subscribe
import com.google.firebase.database.Transaction; //导入依赖的package包/类
@Override public void subscribe(final CompletableEmitter emitter) {
final Transaction.Handler handler = new Transaction.Handler() {
@Override public Transaction.Result doTransaction(MutableData mutableData) {
try {
return task.apply(mutableData);
} catch (Exception e) {
//TODO: Is this enough?
throw new RuntimeException(e);
}
}
@Override public void onComplete(DatabaseError databaseError, boolean committed,
DataSnapshot dataSnapshot) {
if (!emitter.isDisposed()) {
if (null == databaseError) {
emitter.onComplete();
} else {
emitter.onError(databaseError.toException());
}
}
}
};
ref.runTransaction(handler, fireLocalEvents);
}
示例7: doTransaction
import com.google.firebase.database.Transaction; //导入依赖的package包/类
public void doTransaction(String uid, final OnMilestoneTransaction listener){
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if(user!=null) {
String key;
if(uid==null || uid.isEmpty())
return;
DatabaseReference mRef = mDatabase.child(Constants.FIREBASE_MILESTONES).child(user.getUid()).child(SessionSingleton.getInstance().currentCar.getid()).child(uid);
mRef.runTransaction(new Transaction.Handler() {
@Override
public Transaction.Result doTransaction(MutableData mutableData) {
Milestone milestone = mutableData.getValue(Milestone.class);
if(milestone==null)
return Transaction.success(mutableData);
milestone = listener.onTransaction(milestone);
mutableData.setValue(milestone);
return Transaction.success(mutableData);
}
@Override
public void onComplete(DatabaseError databaseError, boolean b, DataSnapshot dataSnapshot) {
}
});
}
}
示例8: incrementUserSongRequestCount
import com.google.firebase.database.Transaction; //导入依赖的package包/类
public void incrementUserSongRequestCount(String partyTitle, String userId) {
databaseReference
.child(partyTitle)
.child(FirebaseConstants.CHILD_USERS)
.child(userId)
.child(FirebaseConstants.CHILD_USERS_SONGS_REQUESTED)
.runTransaction(new Transaction.Handler() {
@Override
public Transaction.Result doTransaction(MutableData currentData) {
if (currentData.getValue() == null) {
currentData.setValue(1);
}
else {
currentData.setValue((Long) currentData.getValue() + 1);
}
return Transaction.success(currentData);
}
@Override
public void onComplete(DatabaseError databaseError, boolean b, DataSnapshot dataSnapshot) {
}
});
}
示例9: setLeader
import com.google.firebase.database.Transaction; //导入依赖的package包/类
@Override
public void setLeader(String deckId, final String leaderId) {
DatabaseReference deckReference = mUserReference.child(deckId).child("leaderId");
// Transactions will ensure concurrency errors don't occur.
deckReference.runTransaction(new Transaction.Handler() {
@Override
public Transaction.Result doTransaction(MutableData mutableData) {
// Set value and report transaction success.
mutableData.setValue(leaderId);
return Transaction.success(mutableData);
}
@Override
public void onComplete(DatabaseError databaseError, boolean b,
DataSnapshot dataSnapshot) {
// Do nothing.
}
});
}
示例10: renameDeck
import com.google.firebase.database.Transaction; //导入依赖的package包/类
@Override
public void renameDeck(final String deckId, final String newName) {
DatabaseReference deckReference = mUserReference.child(deckId).child("name");
// Transactions will ensure concurrency errors don't occur.
deckReference.runTransaction(new Transaction.Handler() {
@Override
public Transaction.Result doTransaction(MutableData mutableData) {
// Set value and report transaction success.
mutableData.setValue(newName);
return Transaction.success(mutableData);
}
@Override
public void onComplete(DatabaseError databaseError, boolean b,
DataSnapshot dataSnapshot) {
// Do nothing.
}
});
}
示例11: deleteDeck
import com.google.firebase.database.Transaction; //导入依赖的package包/类
@Override
public void deleteDeck(final String deckId) {
DatabaseReference deckReference = mUserReference.child(deckId).child("deleted");
// Transactions will ensure concurrency errors don't occur.
deckReference.runTransaction(new Transaction.Handler() {
@Override
public Transaction.Result doTransaction(MutableData mutableData) {
// Set value and report transaction success.
mutableData.setValue(true);
return Transaction.success(mutableData);
}
@Override
public void onComplete(DatabaseError databaseError, boolean b,
DataSnapshot dataSnapshot) {
// Do nothing.
}
});
}
示例12: runTransactionOnComplete
import com.google.firebase.database.Transaction; //导入依赖的package包/类
private void runTransactionOnComplete(Transaction.Handler handler, DatabaseError error,
boolean committed, DataSnapshot snapshot) {
try {
handler.onComplete(error, committed, snapshot);
} catch (Exception e) {
operationLogger.error("Exception in transaction onComplete callback", e);
}
}
示例13: onStarClicked
import com.google.firebase.database.Transaction; //导入依赖的package包/类
private void onStarClicked(DatabaseReference postRef) {
postRef.runTransaction(new Transaction.Handler() {
@Override
public Transaction.Result doTransaction(MutableData mutableData) {
Post p = mutableData.getValue(Post.class);
if (p == null) {
return Transaction.success(mutableData);
}
if (p.stars.containsKey(getUid())) {
// Unstar the post and remove self from stars
p.starCount = p.starCount - 1;
p.stars.remove(getUid());
} else {
// Star the post and add self to stars
p.starCount = p.starCount + 1;
p.stars.put(getUid(), true);
}
// Set value and report transaction success
mutableData.setValue(p);
return Transaction.success(mutableData);
}
@Override
public void onComplete(DatabaseError databaseError, boolean b,
DataSnapshot dataSnapshot) {
// Transaction completed
Log.d(TAG, "postTransaction:onComplete:" + databaseError);
}
});
}
示例14: runTransaction
import com.google.firebase.database.Transaction; //导入依赖的package包/类
/**
* Run a transaction on the data at this location. For more information on running transactions, see
*
* @param ref reference represents a particular location in your database.
* @param fireLocalEvents boolean which allow to receive calls of your transaction in your local device.
* @param transactionValue value of the transaction.
* @return a {@link Single} which emits the final {@link DataSnapshot} value if the transaction success.
*/
@NonNull
public static Single<DataSnapshot> runTransaction(@NonNull final DatabaseReference ref,
@NonNull final boolean fireLocalEvents,
@NonNull final long transactionValue) {
return Single.create(new SingleOnSubscribe<DataSnapshot>() {
@Override public void subscribe(final SingleEmitter emitter) throws Exception {
ref.runTransaction(new Transaction.Handler() {
@Override
public Transaction.Result doTransaction(MutableData mutableData) {
Integer currentValue = mutableData.getValue(Integer.class);
if (currentValue == null) {
mutableData.setValue(transactionValue);
} else {
mutableData.setValue(currentValue + transactionValue);
}
return Transaction.success(mutableData);
}
@Override
public void onComplete(DatabaseError databaseError, boolean b, DataSnapshot dataSnapshot) {
if (databaseError != null) {
emitter.onError(new RxFirebaseDataException(databaseError));
} else {
emitter.onSuccess(dataSnapshot);
}
}
}, fireLocalEvents);
}
});
}
示例15: transaction
import com.google.firebase.database.Transaction; //导入依赖的package包/类
/**
* {@inheritDoc}
*/
@Override
@SuppressWarnings("unchecked")
public <T, R extends T> void transaction(final Class<T> dataType, final TransactionCallback<R> transactionCallback, final CompleteCallback completeCallback)
{
databaseReference().runTransaction(new Transaction.Handler() {
@Override
public Transaction.Result doTransaction(MutableData mutableData)
{
R transactionData = null;
if (dataType == List.class || dataType == Map.class) {
transactionData = (R) mutableData.getValue();
} else {
transactionData = (R) mutableData.getValue(dataType);
}
mutableData.setValue(transactionCallback.run(transactionData));
return Transaction.success(mutableData);
}
@Override
public void onComplete(DatabaseError databaseError, boolean b, DataSnapshot dataSnapshot)
{
if (completeCallback == null) return;
if (databaseError != null) {
completeCallback.onError(databaseError.toException());
} else {
completeCallback.onSuccess();
}
}
});
terminateOperation();
}