我正在使用Parse并在两个模型之间创建了一对一的关系(一个位置有一个队列)。如何仅使用位置来检索队列的属性?
问问题
1067 次
2 回答
2
我刚开始使用 Parse。根据他们的Android 文档,您需要先将队列 ParseObject 添加到位置 ParseObject,然后再存储它们(反之亦然)。
假设您将两者之间的关系放在 location 对象中,您应该能够使用以下内容拉取队列:
存储:
// 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!");
}
}
});
于 2013-01-25T03:14:54.473 回答
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”列来检查哪个队列与哪个位置相关联。
于 2013-01-25T03:40:35.483 回答