我正在使用Parse并在两个模型之间创建一对一的关系(一个位置有一个队列)。如何仅使用位置检索队列的属性?
答案 0 :(得分:2)
我刚开始使用Parse。根据他们的Android documentation,您需要在存储它们之前将队列ParseObject添加到位置ParseObject(反之亦然)。
假设你把两者之间的关系放在了位置对象中,你应该能够用这样的东西拉出队列:
<强>存储强>
// Create location
ParseObject location = new ParseObject("Location");
location.put("foo", "bar");
// Create queue
ParseObject queue = new ParseObject("Queue");
queue.put("name", "Ben");
// Store the queue in the location (location will contain a pointer to queue)
location.put("Queue", queue);
// Save both location and queue
location.saveInBackground();
<强>检索:强>
// Retrieve location using objectId
ParseQuery query = new ParseQuery("Location");
query.getInBackground("QkKt30WhIA", new GetCallback() { // objectId!
public void done(ParseObject object, ParseException e) {
if (e == null) {
// Location found! Query for the queue
object.getParseObject("Queue").fetchIfNeededInBackground(new GetCallback() {
public void done(ParseObject object, ParseException e) {
// Queue found! Get the name
String queueAttr = object.getString("name");
Log.i("TEST", "name: " + queueAttr);
}
});
}
else {
// something went wrong
Log.e("TEST", "Oops!");
}
}
});
答案 1 :(得分:0)
这就是我最终做的事情:
// create qLocation
ParseObject qLocation = new ParseObject("QLocations");
qLocation.put("name", qname);
qLocation.put("streetAddress", streetAddress);
qLocation.put("cityState", cityState);
// create qLine
ParseObject qLine = new ParseObject("Lines");
Random r = new Random();
qLine.put("length", r.nextInt(10)); //set line length to a random number between 0-10
qLine.put("qName",qname);
// add relationship
qLine.put("parent", qLocation);
// save line and location
qLine.saveInBackground();
最后,行qline.put(“parent”,qLocation);因为我不知道如何使用该关系从给定位置获取队列,所以最终并不重要。所以我最后使用qLine中的“qName”列来检查哪个队列与哪个位置相关联。