2013-10-30 40 views
3

當我創建對象的文檔中指定接收對象,我填寫自己的屬性,並保存在數據存儲中的鏈接:這是不可能根據鏈路

public class InvitationEntity extends GenericJson { 
    @Key 
    public String objectId; 
    @Key 
    public int status; 
    @Key("_id") 
    public String id; 
    @Key 
    public String name; 
} 
public class EventEntity extends GenericJson { 
    @Key 
    public String name; 
    @Key 
    public String eventDate; 
    @Key 
    public String location; 
    @Key 
    public String tip; 
    @Key 
    public KinveyReference invitations; 

    public void initReference(InvitationEntity myInvitation){ 
     KinveyReference reference = new KinveyReference("invitationCollection", myInvitation.get("_id").toString()); 
     this.invitations = reference; 
    } 
} 

... 

InvitationEntity invitationEntity = new InvitationEntity(); 
invitationEntity.name = "3"; 
invitationEntity.objectId="3"; 
mKinveyClient.appData("invitationCollection", InvitationEntity.class).save(invitationEntity, new KinveyClientCallback<InvitationEntity>() { 
       @Override 
       public void onSuccess(InvitationEntity invitationEntity1) { 
        Log.e("my", "Ok1 " + invitationEntity1.toString()); 

        EventEntity eventEntity = new EventEntity(); 
        eventEntity.tip = "33"; 
        eventEntity.initReference(invitationEntity1); 
        mKinveyClient.appData("eventCollection", EventEntity.class).save(eventEntity, new KinveyClientCallback<EventEntity>() { 
         @Override 
         public void onSuccess(EventEntity eventEntity_rez) { 
          Log.e("my", "Ok2 " + eventEntity_rez.toString()); 
         } 

         @Override 
         public void onFailure(Throwable t) { 
          Log.e("my", "Failed to save entity " + t.getMessage()); 
         } 
        }); 
       } 

       @Override 
       public void onFailure(Throwable t) { 
        Log.e("my", "Failed to save entity " + t.getMessage()); 
       } 
      }); 

現在我做的請求:

Query q = mKinveyClient.query(); 
    mKinveyClient.appData("eventCollection", EventEntity .class).get(q, new KinveyListCallback<EventEntity >() { 
     @Override 
     public void onSuccess(EventEntity [] resalt) { 
       Log.d("my", resalt[0].toString()); 
     } 

     @Override 
     public void onFailure(Throwable t) { 
      Log.d("my", "Error fetching data: " + t.getMessage()); 
     } 
    }); 

我得到的結果:

{"invitations":{"_collection":"invitationCollection", 
          "_id":"52715e5b13dde23677021c80", 
          "_type":"KinveyRef"}, 
"tip":"33", 
"_acl":{"creator":"526182566bbfcbf429000518"}, 
"_kmd":{"lmt":"2013-10-30T19:30:36.086Z", 
       "ect":"2013-10-30T19:30:36.086Z"}, 
"_id":"52715e5c13dde23677021c81", 
"kid":"kid_TTpvqRShiO", 
"collection":"eventCollection"} 

它希望得到:

{"invitations":{"_id":"52715e5b13dde23677021c80", 
          "name":"3", 
          "objectId":"3", 
          "status":0, 
          "_acl":{"creator":"526182566bbfcbf429000518"}, 
          "_kmd":{"lmt":"2013-10-30T19:30:35.912Z", 
               "ect":"2013-10-30T19:30:35.912Z"}, 
               "kid":"kid_TTpvqRShiO", 
               "collection":"invitationCollection"}, 
"tip":"33", 
"_acl":{"creator":"526182566bbfcbf429000518"}, 
"_kmd":{"lmt":"2013-10-30T19:30:36.086Z", 
       "ect":"2013-10-30T19:30:36.086Z"}, 
"_id":"52715e5c13dde23677021c81", 
"kid":"kid_TTpvqRShiO", 
"collection":"eventCollection"} 

如何接收對象而不是其中的鏈接對象?

+0

幫助,請如何使使鏈接用實物代替? – user2939013

回答

1

我在Kinvey一名工程師,可以幫助您與this--我貼在我們的support forums相同的答案,但想通,別人可能會在這裏絆倒:

這不是不可能的!

在Appdata上,Get和GetEntity方法有多種變體。看看這裏的javadoc:http://devcenter.kinvey.com/android/reference/api/reference/com/kinvey/android/AsyncAppData.html

要解決KinveyReferences,通過一個String []包含要解決的第二個參數,以get方法的字段的名稱,你的情況,你要使用:

mKinveyClient.appData("eventCollection", EventEntity .class).get(q, new String[]{"invitations"}, new KinveyListCallback() { ... } 

現在,回調的onSuccess將返回EventEntity,但引用解決,您可以致電:

InvitationEntity myInvite = results[0].getInvitations.getTypedObject(InvitationEntity.class);