2016-11-07 114 views

回答

3

我建议使用此:

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) 
        { 
         System.out.println("Copy failed"); 
        } 
        else 
        { 
         System.out.println("Success"); 
        } 
       } 
      }); 
     } 

     @Override 
     public void onCancelled(FirebaseError firebaseError) 
     { 
      System.out.println("Copy failed"); 
     } 
    }); 
} 

这来自这一来源:https://gist.github.com/katowulf/6099042。我在我的JavaEE代码中以及我的android应用程序中多次使用过它。

你传递你的fromPath和toPath。这是一个拷贝而不是一个动作,所以原稿也会保留在原来的位置。如果你想删除,你可以在System.out.println(“Success”)后面的fromPath上设置一个值。 。

+0

它工作正常,但它使用firebase url作为路径(ref)并移动所有孩子。我想只选择一个孩子。如何只移动一个孩子。 TIA –

+0

如果您有孩子的ID,那么您可以从一个ID直接移动到另一个ID。 –

+0

它会:myfirebase.firebase.com/initialPlace/ID到myfirebase.firebase.com/finalPlace/ID。您可以将任何字符串作为变量传递给Firebase构造函数。 –

3

由于编译火力数据库的:11.0.1,这是与新的类引用(https://firebase.google.com/support/guides/firebase-android 2017年7月5日)

private void moveGameRoom(final DatabaseReference fromPath, final DatabaseReference toPath) { 
     fromPath.addListenerForSingleValueEvent(new ValueEventListener() { 
      @Override 
      public void onDataChange(DataSnapshot dataSnapshot) { 
       toPath.setValue(dataSnapshot.getValue(), new DatabaseReference.CompletionListener() { 
        @Override 
        public void onComplete(DatabaseError firebaseError, DatabaseReference firebase) { 
         if (firebaseError != null) { 
          System.out.println("Copy failed"); 
         } else { 
          System.out.println("Success"); 

         } 
        } 
       }); 

      } 

      @Override 
      public void onCancelled(DatabaseError databaseError) { 

      } 
     }); 
    } 
0

如果要执行这一举动也消除原稿相同功能,你可以使用下面的代码片段:

// In this piece of code, "fromPath" and "toPath" parameters act like directories. 
private void removeFromFirebase(final DatabaseReference fromPath, final DatabaseReference toPath, final String key) { 
    fromPath.child(key).addListenerForSingleValueEvent(new ValueEventListener() { 
     // Now "DataSnapshot" holds the key and the value at the "fromPath". 
     // Let's move it to the "toPath". This operation duplicates the 
     // key/value pair at the "fromPath" to the "toPath". 
     @Override 
     public void onDataChange(DataSnapshot dataSnapshot) { 
      toPath.child(dataSnapshot.getKey()) 
        .setValue(dataSnapshot.getValue(), new DatabaseReference.CompletionListener() { 
         @Override 
         public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) { 
          if (databaseError == null) { 
           Log.i(TAG, "onComplete: success"); 
           // In order to complete the move, we are going to erase 
           // the original copy by assigning null as its value. 
           fromPath.child(key).setValue(null); 
          } 
          else { 
           Log.e(TAG, "onComplete: failure:" + databaseError.getMessage() + ": " 
             + databaseError.getDetails()); 
          } 
         } 
        }); 
     } 

     @Override 
     public void onCancelled(DatabaseError databaseError) { 
      Log.e(TAG, "onCancelled: " + databaseError.getMessage() + ": " 
        + databaseError.getDetails()); 
     } 
    }); 
} 
相关问题