2014-07-01 86 views
0

使用iOS Parse SDK設置新PFObject作爲另一個新的PFObject的鍵值,我可以成功地創建一個新的PFObject A並將其設置爲另一個新PFOBject B值:解析 - 使用REST API

PFObject * objectA = [PFObject objectWithClassName:@"objectA"]; 
[objectA setValue:@"Test Text" forKey:@"text"]; 

PFObject * objectB = [PFObject objectWithClassName:@"objectB"]; 
[objectB setValue:objectA forKey:@"objectA"]; 

當我執行[PFObject saveAll:(NSArray*)objects]時,Parse將爲objectA生成objectId,並將其設置爲objectB上的密鑰中的指針值。

我該怎麼用REST API (PYTHON)來做到這一點。從文檔,看來我必須出示在關鍵指針傳遞時,作爲一種價值爲我的類objectB如下:

{"__type": "Pointer", 
"className": "objectA", 
"objectId":objectA.get('objectId')} 

然而,當我創建一個新的objectA,該objectId還不存在。

這是我的Python代碼:

def generateFakeMessages(): 
    messageFactory = MessageFactory(retrieveUsers()) 

    appKeys = { 
     "X-Parse-Application-Id": "XXXX", 
     "X-Parse-REST-API-Key": "XXXX", 
     "Content-Type": "application/json"} 

    mList = [] 
    objectA = messageFactory.fakeMessage() 
    arrayOfObjectBs = messageFactory.fakeRecipients() 

    mList.append({ 
      "method": "POST", 
      "path": "/1/classes/testMessageClass", 
      "body": objectA 
      }) 
    for objectB in arrayOfObjectBs: 
     objectB['objectAPointer'] = { 
       "__type": "Pointer", 
       "className": "testMessageClass", 
       "objectId": objectA.get('objectId') 
       } 
     mList.append({ 
      "method": "POST", 
      "path": "/1/classes/recipients", 
      "body": objectB 
       }) 

    requests = {"requests":mList} 
    connection = httplib.HTTPSConnection('api.parse.com', 443) 
    connection.connect() 
    connection.request('POST', '/1/batch', json.dumps(requests), appKeys) 
    result = json.loads(connection.getresponse().read()) 
    print result 

回答

0

作爲每this question on the Parse forumsaveAll:在後臺使用batches,如果它確定所述對象圖具有它它們在間歇未保存的兒童,然後使用結果這是爲了能夠正確保存父母。

要使用REST API自己做到這一點,您需要自己完成批處理。每個批次的批次數限制爲50個創建/更新/刪除操作。

如果您有50個objectA實例,每個實例都有一個引用它的objectB,那麼您可以分兩批進行操作。

+0

我加了我以前的代碼。 – blackirishman

0

這是使用批次更正的代碼。而不是設置指針表示

objectB['message'] 

我設置了實際的對象。 Parse對上傳的依賴進行排序。

def generateFakeMessages(): 
    messageFactory = MessageFactory(retrieveUsers()) 

    appKeys = { 
     "X-Parse-Application-Id": "XXXX", 
     "X-Parse-REST-API-Key": "XXXX", 
     "Content-Type": "application/json"} 

    mList = [] 
    objectA = messageFactory.fakeMessage() 
    arrayOfObjectBs = messageFactory.fakeRecipients() 

    mList.append({ 
      "method": "POST", 
      "path": "/1/classes/testMessageClass", 
      "body": objectA 
      }) 
    for objectB in arrayOfObjectBs: 
     objectB['message'] = fakeMessage 
     mList.append({ 
      "method": "POST", 
      "path": "/1/classes/recipients", 
      "body": objectB 
       }) 


    requests = {"requests":mList} 
    connection = httplib.HTTPSConnection('api.parse.com', 443) 
    connection.connect() 
    connection.request('POST', '/1/batch', json.dumps(requests), appKeys) 
    result = json.loads(connection.getresponse().read()) 
    print result