我一直在使用sql,现在我正在使用解析。我遇到了有关数据库结构的问题。我有2个对象叫做商店和用户。我已经将商店objectId添加到每个用户,以便我可以识别哪个用户是哪个商店。我不知道是否还有其他办法,但这是我能想到的。
问题是我想要检索连接到用户的所有用户和商店名称。如何使用解析实现这个?
目前我的代码。
PFQuery *query = [PFQuery queryWithClassName:@"users"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
for (PFObject *object in objects) {
}
NSLog(@"%@", objects);
} else {
// Log details of the failure
NSLog(@"Error: %@ %@", error, [error userInfo]);
}
}];
}
答案 0 :(得分:0)
如果我理解正确,您只想获取相关对象? The documentation covers this
你应该能够获取这样的相关对象:
PFObject *store = user[@"store"]; // reference to related object but may need to be fetched
[store fetchIfNeededInBackgroundWithBlock:^(PFObject *object, NSError *error) {
// at this point you have fetched the store or you already have it
NSString *name = store[@"name"];
}];
答案 1 :(得分:0)
听起来的目的是获取相关对象,但我对如何做到这一点的理解与@Dima不同。你这样做......
PFQuery *query = [PFQuery queryWithClassName:@"users"];
// this tells the query to eagerly fetch the related store object
[query includeKey:@"store"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
// handle error, or ...
for (PFObject *object in objects) {
// object is from the users table, and object[@"store"] will contain
// the complete, related store object, eagerly fetched...
NSLog(@"the store object for %@ is %@", object, object[@"store"]);
}
}];