function copyFbRecord(oldRef, newRef) { | |
oldRef.once('value', function(snap) { | |
newRef.set( snap.value(), function(error) { | |
if( error && typeof(console) !== 'undefined' && console.error ) { console.error(error); } | |
}); | |
}); | |
} |
function moveFbRecord(oldRef, newRef) { | |
oldRef.once('value', function(snap) { | |
newRef.set( snap.value(), function(error) { | |
if( !error ) { oldRef.remove(); } | |
else if( typeof(console) !== 'undefined' && console.error ) { console.error(error); } | |
}); | |
}); | |
} |
This comment has been minimized.
This comment has been minimized.
Yes, this works with my version: function moveFbRecord(oldRef, newRef) {
oldRef.once('value', function(snap) {
newRef.set( snap.val(), function(error) {
if( !error ) { oldRef.remove(); }
else if( typeof(console) !== 'undefined' && console.error ) { console.error(error); }
});
});
} |
This comment has been minimized.
This comment has been minimized.
can i have the java implementation of this? Thanks in advance |
This comment has been minimized.
This comment has been minimized.
Java implementation public void moveFirebaseRecord(Firebase fromPath, final Firebase toPath)
{
fromPath.addListenerForSingleValueEvent(new ValueEventListener()
{
@Override
public void onDataChange(DataSnapshot dataSnapshot)
{
toPath.setValue(dataSnapshot.getValue(), new Firebase.CompletionListener()
{
@Override
public void onComplete(FirebaseError firebaseError, Firebase firebase)
{
if (firebaseError != null)
{
Timber.d("moveFirebaseRecord() failed. firebaseError = %s", firebaseError);
}
else
{
Timber.d("moveFirebaseRecord() Great success!");
}
}
});
}
@Override
public void onCancelled(FirebaseError firebaseError)
{
Timber.d("moveFirebaseRecord() failed. firebaseError = %s", firebaseError);
}
});
} |
This comment has been minimized.
This comment has been minimized.
Is that Java code a Move or Copy? It appears to me to be a Copy, as there is no .remove() ? |
This comment has been minimized.
This comment has been minimized.
Are these functions still valid for Firebase 3?
|
This comment has been minimized.
This comment has been minimized.
This line reminds me of the reason StringUtils was created :)
|
This comment has been minimized.
This comment has been minimized.
instead of
you may want to do:
if you want to ADD the data instead of REPLACING all your existing child with the data you want to move. |
This comment has been minimized.
This comment has been minimized.
Any way to easily copy and preserve $priority for each item? Although, I have read somewhere that using $priority is not recommended anymore. However, it's useful for enabling sorting/ordering of key-value pairs in an object that absolutely need both key and value to make references to another object. |
This comment has been minimized.
This comment has been minimized.
Firebase 3.x and Promise version of this. https://gist.github.com/jofftiquez/f60dc81b39d77cd4eb1f5b5cbe6585ad |
This comment has been minimized.
This comment has been minimized.
I'm looking for a Cloud Firestore version of copy and move. Any ideas? |
This comment has been minimized.
snap.val() instead of snap.value() might be better in the current version of Firebase.